Pagini recente » Cod sursa (job #2762333) | Cod sursa (job #2827311) | Cod sursa (job #1660903) | Cod sursa (job #913999) | Cod sursa (job #442409)
Cod sursa(job #442409)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 14, 2010, 1:53 PM
*/
#include <queue>
#include <cstdlib>
#include <fstream>
#define Nmax 1000
#define oo 0x3f3f3f3f
/*
*
*/
using namespace std;
int N, source, sink;
int C[Nmax][Nmax];
vector< int > c, f;
vector< vector< int > > G;
vector< int >::const_iterator it, iend;
inline int find_path( void )
{
int x;
queue< int > Q;
f.assign( N, -1 );
f[source]=-2;
c[source]=oo;
for( Q.push(source); -1 == f[sink] && !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;
c[*it]=min( c[x], C[x][*it] );
Q.push( *it );
}
}
if( -1 == f[sink] )
return 0;
for( x=sink; -2 != f[x]; x=f[x] )
{
C[x][f[x]]+=c[sink];
C[f[x]][x]-=c[sink];
}
return c[sink];
}
inline int MaxFlow( void )
{
int s, p;
for( s=0, p=find_path(); p; s+=p, p=find_path() );
return s;
}
int main(int argc, char** argv)
{
int M, x, y;
ifstream in( "maxflow.in" );
in>>N>>M;
sink=N-1;
G.resize(N);
c.resize(N);
f.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<<MaxFlow()<<'\n';
return EXIT_SUCCESS;
}