Cod sursa(job #405770)

Utilizator alexandru92alexandru alexandru92 Data 28 februarie 2010 18:50:26
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <queue>
#include <vector>
#include <fstream>
#define INF 0x3f3f3f3f

/*
 *
 */
using namespace std;
int N;
int C[1000][1000];
vector< vector< int > > G;
int find_path( void )
{
	int x;
	queue< int > Q;
	vector< int >  d( N+1 ), father( N+1, -1 );
	vector< int >::const_iterator it, iend;
	Q.push( 0 );
	d[0]=INF;
	while( -1 == father[N] && !Q.empty() )
	{
		x=Q.front(); Q.pop();
		for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
			if( -1 == father[*it] && C[x][*it] > 0 )
			{
				Q.push(*it);
				father[*it]=x;
				d[*it]=min( d[x], C[x][*it] );
			}
	}
	if( -1 == father[N] )
		return -1;
	father[0]=-1;
	for( x=N; -1 != father[x]; x=father[x] )
	{
		C[father[x]][x]-=d[N];
		C[x][father[x]]+=d[N];
	}
	return d[N];
}
int Maximum_Flow( void )
{
	int path_c, s=0;
	for( path_c=find_path(); -1 != path_c; s+=path_c, path_c=find_path() );
	return s;
}
int main( void )
{
	int M, x, y;
	ifstream in( "maxflow.in" );
	in>>N>>M;
	G.resize(N);
	--N;
	for( ; M; --M )
	{
		in>>x>>y;
		--x, --y;
		in>>C[x][y];
		G[x].push_back(y);
		G[y].push_back(x);
	}
	ofstream out( "maxflow.out" );
	out<<Maximum_Flow();
	return 0;
}