Pagini recente » Cod sursa (job #649041) | Cod sursa (job #788810) | Cod sursa (job #3265588) | Cod sursa (job #2914304) | Cod sursa (job #460706)
Cod sursa(job #460706)
#include <queue>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#define Nmax 1111
/*
*
*/
using namespace std;
queue< int > Q;
int N;
int f[Nmax];
int C[Nmax][Nmax], F[Nmax][Nmax];
vector< int > G[Nmax];
vector< int >::const_iterator it, iend;
inline bool BFS( void )
{
int x;
fill( f+2, f+N+1, -1 );
f[1]=0;
for( Q.push(1); !Q.empty(); )
{
x=Q.front(); Q.pop();
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
if( -1 == f[*it] && C[x][*it] > F[x][*it] )
{
f[*it]=x;
Q.push( *it );
}
}
return -1 != f[N];
}
int main( void )
{
int M, x, y, s, c;
ifstream in( "maxflow.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
in>>C[x][y];
G[x].push_back(y);
G[y].push_back(x);
}
for( s=0; BFS(); )
{
for( x=1; x <= N; ++x )
if( C[x][N] > F[x][N] )
{
c=C[x][N]-F[x][N];
for( y=x; f[y]; y=f[y] )
c=min( c, C[f[y]][y]-F[f[y]][y] );
s+=c;
F[x][N]+=c;
F[N][x]-=c;
for( y=x; f[y]; y=f[y] )
{
F[f[y]][y]+=c;
F[y][f[y]]-=c;
}
}
}
ofstream out( "maxflow.out" );
out<<s<<'\n';
return EXIT_SUCCESS;
}