Cod sursa(job #428875)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 29 martie 2010 17:15:52
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <cstdio>
#include <vector>


#define nmax 1005
#define INF 0x3f3f3f
#define pb push_back

using namespace std;


int dad [nmax], c [nmax * nmax], F [nmax][nmax], C [nmax][nmax];
vector <int> G [nmax];
bool viz [nmax];
int n, maxflow, flow;
inline bool bfs ()
{	
	int node, p, u, V;
	memset (viz, 0, sizeof (viz));
	viz [1] = true;
	p = u = 0;
	c [p] = 1;
	vector <int> :: iterator it;
	while (p <= u)
	{	
		node = c [p++];
		if (node == n) return viz [n];	
		for (it = G [node].begin (); it != G [node].end (); it++)
		{
			V = *it;
			if (!viz [V] && F [node][V] < C [node][V]) //muchie nesaturata
			{	
				viz [V] = true;
				c [++u] = V;
				dad [V] = node;
			}
		}
	}
	//printf ("%d\n", viz [n]);
	return viz [n];
}		
int main ()
{
	freopen ("maxflow.in", "r", stdin);
	freopen ("maxflow.out", "w", stdout);
	int m, x, y, z, node;
	scanf ("%d%d\n", &n, &m);
	while (m--)
	{
		scanf ("%d%d%d\n", &x, &y, &z);
		G [x].pb (y);
		G [y].pb (x);
		C [x][y] = z; //capacity
	}
	vector <int> :: iterator it; //not from Italia :)
	for (maxflow = 0; bfs (); )
		for (it = G [n].begin (); it != G [n].end (); it++)
		{
			node = *it;
			if (F [node][n] < C [node][n] && viz [node])
			{	
				dad [n] = node;
				flow = INF;
				for (node = n; node != 1; node = dad [node])
					if (flow > C [dad [node]][node] - F [dad [node]][node])
						flow = C [dad [node]][node] - F [dad [node]][node];
				if (flow)
				{
					for (node = n; node != 1; node = dad [node]) 
					{
						F [dad [node]][node] += flow; 		
						F [node][dad [node]] -= flow;
					}
					maxflow += flow;
				}
			}
		}
	printf ("%d\n", maxflow);
	return 0;
}