Cod sursa(job #275700)

Utilizator ilincaSorescu Ilinca ilinca Data 10 martie 2009 16:59:03
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <stdio.h>
#include <string.h>

#define nmax 1005
#define inf 110005


int n, m, t [nmax], c [nmax] [nmax], f [nmax] [nmax], l [nmax] [nmax<<1];



inline int min (int a, int b)
{
	if (a < b)
		return a;
	return b;
}

void scan ()
{
	int i, x, y, cap;
	scanf ("%d%d", &n, &m);
	for (i=1; i <= m; ++i)
	{
		scanf ("%d%d%d", &x, &y, &cap);
		c [x] [y]=c [y] [x]=cap;
		l [x] [++l [x] [0]]=y;
        l [y] [++l [y] [0]]=x;
	}
}

int BFS ()
{
	int p, u, i, nod, q [nmax];
	memset (t, 0, sizeof (t));
	p=u=1;
	q [p]=1;
	t [1]=-1;
	while (p <= u)
	{
		nod=q [p];
		for (i=1; i <= l [nod] [0]; ++i)
			if ((!t [l [nod] [i]]) && (c [nod] [l [nod] [i]] - f [nod] [l [nod] [i]] > 0))
			{
				t [l [nod] [i]]=nod;
				q [++u]=l [nod] [i];
				if (q [u] == n)
					return 1;
			}
		++p;
	}
	return 0;
}

int flux ()
{
	int F, i, cr;
	for (F=0; BFS (); F+=cr)
	{
		cr=inf;
		for (i=n; i != 1; i=t [i])
			cr=min (cr, c [t [i]] [i]-f [t [i]] [i]);

		for (i=n; i != 1; i=t [i])
		{
			f [t [i]] [i]+=cr;
			f [i] [t [i]]-=cr;
		}
	}
	return F;
}


int main ()
{
	freopen ("maxflow.in", "r", stdin);
	freopen ("maxflow.out", "w", stdout);
	scan ();
	printf ("%d\n", flux ());
	return 0;
}