Cod sursa(job #405625)

Utilizator alexandru92alexandru alexandru92 Data 28 februarie 2010 14:00:15
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <queue>
#include <vector>
#include <fstream>

/*
 *
 */
using namespace std;
int N;
int C[1000][1000];
vector< vector< int > > G;
int find_path( void )
{
	int min_c=0, x;
	queue< int > Q;
	vector< int > father( N+1, -1 );
	vector< int >::const_iterator it, iend;
	Q.push( 0 );
	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;
			}
	}
	if( -1 == father[N] )
		return -1;
	father[0]=-1;
	min_c=C[father[N]][N];
	for( x=father[N]; -1 != father[x]; x=father[x] )
		min_c=min( min_c, C[father[x]][x] );
	for( x=N; -1 != father[x]; x=father[x] )
	{
		C[father[x]][x]-=min_c;
		if( !C[x][father[x]] )
			G[x].push_back( father[x] );
		C[x][father[x]]+=min_c;
	}
	return min_c;
}
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);
	}
	ofstream out( "maxflow.out" );
	out<<Maximum_Flow();
	return 0;
}