Pagini recente » Cod sursa (job #3276689) | Cod sursa (job #1218441) | Cod sursa (job #2971033) | Cod sursa (job #712674) | Cod sursa (job #442595)
Cod sursa(job #442595)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 14, 2010, 8:42 PM
*/
#include <queue>
#include <cstdlib>
#include <fstream>
#define Nmax 1001
/*
*
*/
using namespace std;
int C[Nmax][Nmax];
vector< int > f, d;
vector< vector< int > > G;
vector< int >::const_iterator it, iend;
inline int find_path( void )
{
int x, N=f.size();
queue< int > Q;
f.assign( N, -1 );
f[0]=-2;
d[0]=0x3f3f3f3f;
for( Q.push(0), N-=1; -1 == f[N] && !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] > 0 )
{
f[*it]=x;
d[*it]=min( d[x], C[x][*it] );
Q.push(*it);
}
}
if( -1 == f[N] )
return 0;
for( x=N; -2 != f[x]; x=f[x] )
{
C[x][f[x]]+=d[N];
C[f[x]][x]-=d[N];
}
return d[N];
}
inline int MaximumFlow( void )
{
int s, p;
for( s=0, p=find_path(); p; s+=p, p=find_path() );
return s;
}
int main( void )
{
int N, M, x, y;
ifstream in( "maxflow.in" );
in>>N>>M;
G.resize(N);
f.resize(N);
d.resize(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<<MaximumFlow()<<'\n';
return EXIT_SUCCESS;
}