Pagini recente » Cod sursa (job #1366950) | Cod sursa (job #2644895) | Cod sursa (job #1724186) | Cod sursa (job #3235871) | Cod sursa (job #406255)
Cod sursa(job #406255)
#include <queue>
#include <vector>
#include <fstream>
#define INF 0x3f3f3f3f
/*
*
*/
using namespace std;
int N, ss, ds;
int C[1000][1000];
vector< int > d, father;
vector< vector< int > > G;
vector< int >::const_iterator it, iend;
inline const int& min( const int& x, const int& y )
{
if( x < y )
return x;
return y;
}
int find_path( void )
{
int x;
queue< int > Q;
d[0]=INF;
father.assign( N, -1 );
Q.push( ss );
while( -1 == father[ds] && !Q.empty() )
{
x=Q.front(); Q.pop();
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
if( -1 == father[*it] && C[x][*it] )
{
Q.push( *it );
father[*it]=x;
d[*it]=min( d[x], C[x][*it] );
}
}
if( -1 == father[ds] )
return 0;
father[ss]=-1;
for( x=ds; -1 != father[x]; x=father[x] )
{
C[father[x]][x]-=d[ds];
if( 0 == C[x][father[x]] )
G[x].push_back( father[x] );
C[x][father[x]]+=d[ds];
}
return d[ds];
}
int Maximum_Flow( void )
{
int path_c, s=0;
for( path_c=find_path(); 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;
G.resize(N);
d.resize(N);
father.resize(N);
for( ; M; --M )
{
in>>x>>y;
--x, --y;
in>>C[x][y];
G[x].push_back(y);
}
ss=0; ds=N-1;
//in>>ss>>ds;
ofstream out( "maxflow.out" );
out<<Maximum_Flow();
return 0;
}