Cod sursa(job #531114)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 8 februarie 2011 22:13:11
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <cstdio>
#include <vector>
using namespace std;
#define nmax 1010

vector <int> g[nmax];
int n, m, c[nmax][nmax], sol, u[nmax], fm, q[nmax], f[nmax][nmax], t[nmax];

int bf()
{
	int i, y, j, h=1;
	for (i=1; i<=n; i++) u[i]=0;
	q[1]=1;
	u[1]=1;
	for (y=1; y<=h; y++)
	{
		i=q[y];
		for (j=0; j<g[i].size(); j++)
			if (!u[g[i][j]])
				if (c[i][g[i][j]]!=f[i][g[i][j]])
				{
					u[g[i][j]]=1;
					q[++h]=g[i][j];
					t[g[i][j]]=i;
					if (g[i][j]==n) return 1;
				}
	}
	return 0;
}

int main()
{
	freopen("maxflow.in","r",stdin);
	freopen("maxflow.out","w",stdout);
	scanf("%d %d",&n, &m);
	int i, x, y, z, nod;
	for (i=1; i<=m; i++)
	{
		scanf("%d %d %d",&x, &y, &z);
		g[x].push_back(y);
		g[y].push_back(x);
		c[x][y] += z;
	}
	while (bf())
	{
		nod=n;
		fm=1<<30;
		while (t[nod])
		{
			fm = min(fm, c[t[nod]][nod]-f[t[nod]][nod]);
			nod=t[nod];
		}
		nod=n;
		while (t[nod])
		{
			f[t[nod]][nod] += fm;
			f[nod][t[nod]] -= fm;
			nod=t[nod];
		}
		sol+=fm;
	}
	printf("%d\n",sol);
}