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 alpc12 at 2007-01-10 16:03:11 on Problem 1273
#include <stdio.h>
#include <string.h>

const int N = 210;
const int MAXINT = 2000000000;

int m, n;
int c[N][N], f[N][N], fa[N], q[N], S, T;

void init()
{
	int i, u, v, w;
	//initiation
	memset(c, 0, sizeof(c));
	memset(f, 0, sizeof(f));
	//input
	for(i = 0; i<m;	i++) {
		scanf("%d %d %d", &u, &v, &w);
		if(u == v) continue;
		c[u][v] += w;
	}
	//pretreatment
}

void work()
{
	int i;
	int ans = 0; //最大流
	do
	{
		int qs = 0, qe = 1;
		q[0] = S;
		memset(fa, 0, sizeof(fa));
		while(fa[T] == 0 && qs < qe) {
			int cur = q[qs];
			for(i = 2; i <= T; i++) {
				if(fa[i] == 0) {
					if(f[cur][i] < c[cur][i])  	{
						fa[i] = cur;
						q[qe++] = i;
					}
					else if(f[i][cur] > 0)		{
						fa[i] = -cur;
						q[qe++] = i;
					}
				}//if
			}//for
			qs++;
		}//while
		if(fa[T] != 0) {
			int minf = MAXINT;
			i = T;
			while(i != S) {
				int& cur = fa[i];
				if(cur > 0) {
					if(c[cur][i] - f[cur][i] < minf)
						minf = c[cur][i] - f[cur][i];
					i = cur;
				}
				else {
					if(f[i][cur] < minf) 
						minf = f[i][cur];
					i = -cur;
				}
			}
			ans += minf;
			//修正网络流
			int i = T;
			while(i != S) {
				int& cur = fa[i];
				if(cur > 0) {
					f[cur][i] += minf;
					i = cur;
				}
				else {
					f[cur][i] -= minf;
					i = -cur;
				}
			}
		}//if
	}while(fa[T] != 0);
	printf("%d\n", ans);
}

int main()
{
//	freopen("t.in", "r", stdin);
	while(scanf("%d %d", &m, &n)!=EOF) {
		S = 1; T = n;
		init();
		work();
	}
	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