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

第一道网络流题mark

Posted by zerocpp at 2011-03-30 22:49:08 on Problem 1273 and last updated at 2011-03-30 22:49:19
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define M 205
int n;
int s, t;
int c[M][M];
int f[M][M];
int p[M];
bool v[M];
bool aug()
{
	//memset(p,0,sizeof(p));
	queue<int> q;
	q.push(s);
	memset(v,0,sizeof(v));
	v[s] = 1;
	while (!q.empty())
	{
		int qf = q.front(); q.pop();
		for (int i = 1; i <= n; ++ i)
		{
			if (!v[i] && c[qf][i])
			{
				v[i] = 1;
				p[i] = qf;
				q.push(i);
				if (i == t) return 1;
			}
		}
	}
	return 0;
}
int fordFulkerson()
{
	int res = 0;
	memset(f,0,sizeof(f));
	while (1)
	{
		if (!aug()) break;
		int i = t;
		int mr = (1<<30);
		while (i != s)
		{
			if (mr > c[p[i]][i])
				mr = c[p[i]][i];
			i = p[i];
		}
		i = t;
		while (i != s)
		{
			c[p[i]][i] -= mr;
			c[i][p[i]] += mr;
			i = p[i];
		}
		res += mr;
	}
	return res;
}
int main()
{
	int e;
	while (~scanf("%d %d", &e, &n))
	{
		s = 1; t = n;
		memset(c,0,sizeof(c));
		while (e --)
		{
			int a1, a2, a3;
			scanf("%d %d %d", &a1, &a2, &a3);
			c[a1][a2] += a3;
		}
		printf("%d\n", fordFulkerson());
	}
	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