Pagini recente » Cod sursa (job #2947346) | Cod sursa (job #2839517) | Cod sursa (job #408803) | Cod sursa (job #3257603) | Cod sursa (job #405013)
Cod sursa(job #405013)
#include <queue>
#include <vector>
#include <fstream>
/*
*
*/
using namespace std;
int N;
int C[1024][1024];
int find_path( void ) // min_c if there is a agumenting path, -1 else
{
int x, i, min_c;
queue< int > Q;
vector< int > father( N+1, -1 );
Q.push( 0 );
father[0]=-2;
while( -1 == father[N] && !Q.empty() ) //try to find a agumenting path
{
x=Q.front(); Q.pop();
for( i=0; i <= N; ++i )
if( -1 == father[i] && C[x][i] > 0 )
{
Q.push( i );
father[i]=x;
}
}
if( -1 == father[N] ) //if there wasn't any path
return -1;
//if there is a agumenting path
min_c=C[ father[N] ][ N ];
for( i=father[N]; -2 != father[i]; i=father[i] ) //find the min C from the path
min_c=min( min_c, C[ father[i] ][ i ] );
for( i=N; -2 != father[i]; i=father[i] ) //update
{
C[ father[i] ][ i ]-=min_c;
C[ i ][ father[i] ]+=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;
N-=1;
for( ; M; --M )
in>>x>>y, in>>C[x-1][y-1];
ofstream out( "maxflow.out" );
out<<Maximum_Flow();
return 0;
}