Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

请问为什么会出现这种情况???

Posted by fishyuze at 2007-03-27 20:36:11 on Problem 1273
我用bfs写的增广路径,重边也考虑了, 数组开210也够大,但是交上去是RE
我怀疑是数组开小了, 于是开了个500的,就变TLE了.不知道是怎么一回事,请大牛赐教.
附代码:
#include <iostream>
#include <queue>

using namespace std;
#include "memory.h"
#define MAX 210
int cap[MAX][MAX], f[MAX][MAX], path[MAX], cf[MAX][MAX];//f流值,cf残留网络,cap容量
int len, n, m;
bool u[MAX];//是否访问过的标记
int bfs(int from)
{
	int min = 99999999, i;
	std::queue<int> q;
	q.push(from);
	u[0] = true;

	while(!q.empty())
	{
		int next = q.front();
		q.pop();
		
		if(next == n - 1) 
		{
			for(i = n - 1; path[i] != -1; i = path[i])
			{
				min = min > cf[path[i]][i] ? cf[path[i]][i]:min;
			}
			return min;
		}
		for(i = 1; i <= n - 1; i++)
		{
			if(!u[i] && cf[next][i] > 0)
			{
				q.push(i);
				u[i] = true;
				path[i] = next;
			}
		}
	}
	return 0;
}
/*
int dfs(int from)
{
	int i;
	if(from == n)
	{
		int min = 99999999;
		for(i = n; path[i] != -1; i = path[i])
		{
			min = min > cf[path[i]][i] ? cf[path[i]][i]:min;
		}
		return min;
	}
	u[from] = true;
	for(i = 0; i <= n; i++)
	{
		if(!u[i] && cf[from][i] > 0)
		{
			u[i] = true;
			path[i] = from;
			int flow = dfs(i);
			if(flow > 0) return flow;
			path[i] = -1;
			u[i] = false;
		}
	}
	return 0;
}*/
void solve()
{
	int min, i;
	memset(u, false, sizeof(u));
	memset(path, -1, sizeof(path));
	while((min = bfs(0)) != 0)
	{
		for(i = n - 1; i != -1; i = path[i])
		{
			//int from = path[i];
			f[path[i]][i] += min;
			f[i][path[i]] -= min;
			if(f[path[i]][i] > 0)
			{
				cf[i][path[i]] = f[path[i]][i];
				cf[path[i]][i] = cap[path[i]][i] - f[path[i]][i];
			}
		}
		memset(u, false, sizeof(u));
		memset(path, -1, sizeof(path));
	}
	__int64 maxflow = 0;
	for(i = 1; i <= n - 1; i++)
	{
		maxflow += f[0][i];
	}
	printf("%I64d\n", maxflow);
}
void makemap()
{
	//建图,初始化容量矩阵以及残留网络,确定顶点个数n
	memset(f, 0, sizeof(f));
	memset(cap, 0, sizeof(cap));
	memset(cf, 0, sizeof(cf));
	int i, s, e, c;
	for(i = 1; i <= m; i++)
	{
		cin >> s >> e >> c;
		if(cap[s-1][e-1] == 0) cap[s-1][e-1] = cf[s-1][e-1] = c;
		else cap[s-1][e-1] = cf[s-1][e-1] += c;//重边
	}
}
int main()
{
	//freopen("in.txt", "r", stdin);
	while(cin >> m >> n)
	{
		makemap();
		solve();
	}
         return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator