Cod sursa(job #456507)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 15, 2010, 7:15 PM
*/
#include <queue>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#define Nmax 1011
#define oo 0x3f3f3f3f
/*
*
*/
using namespace std;
int N;
int f[Nmax];
int C[Nmax][Nmax];
inline bool find_path( void )
{
int x, y;
queue< int > Q;
fill( f+1, f+N+1, -1 );
f[1]=0;
for( Q.push(1); !Q.empty(); )
{
x=Q.front(); Q.pop();
for( y=1; y <= N; ++y )
if( -1 == f[y] && C[x][y] > 0 )
{
f[y]=x;
Q.push(y);
}
}
return -1 != f[N];
}
int main( void )
{
int M, x, y, c;
ifstream in( "maxflow.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
in>>C[x][y];
}
for( x=0; find_path(); )
{
for( M=1; M < N; ++M )
if( C[M][N] > 0 )
{
c=C[M][N];
for( y=M; f[y]; y=f[y] )
if( c > C[f[y]][y] )
c=C[f[y]][y];
x+=c;
C[M][N]-=c;
C[N][M]+=c;
for( y=M; f[y]; y=f[y] )
{
C[f[y]][y]-=c;
C[y][f[y]]+=c;
}
}
}
ofstream out( "maxflow.out" );
out<<x<<'\n';
}