Pagini recente » Cod sursa (job #1699561) | Cod sursa (job #2136525) | Cod sursa (job #1587312) | Cod sursa (job #2023611) | Cod sursa (job #405074)
Cod sursa(job #405074)
#include <queue>
#include <vector>
#include <fstream>
#define inf 0x3f3f3f3f
/*
*
*/
using namespace std;
struct pr
{
int f, w, c;
pr( void ) : f(-1), w(0), c(inf) { };
pr( int _f, int _w, int _c ) : f(_f), w(_w), c(_c) { };
inline bool operator<( const pr& x ) const
{
return x.c > c;
}
};
int N;
int C[ 1000 ][ 1000 ];
vector< int > father;
vector< vector< int > > G;
vector< int >::const_iterator it, iend;
int find_path( void )
{
pr x;
int i;
father.assign( N+1, -1 );
priority_queue< pr, vector< pr > > PQ;
PQ.push( pr() );
while( !PQ.empty() )
{
x=PQ.top(); PQ.pop();
father[x.w]=x.f;
if( x.w == N )
break;
for( it=G[x.w].begin(), iend=G[x.w].end(); it < iend; ++it )
if( C[x.w][*it] > 0 )
PQ.push( pr( x.w, *it, min( x.c, C[x.w][*it] ) ) );
}
if( -1 == father[N] )
return -1;
father[0]=-1;
for( i=N; -1 != father[i]; i=father[i] )
{
C[ father[i] ][ i ]-=x.c;
C[ i ][ father[i] ]+=x.c;
}
return x.c;
}
int Maximum_Flow( void )
{
int path_c, s=0;
for( path_c=find_path(); -1 != 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 );
N-=1;
for( ; M; --M )
{
in>>x>>y;
--x, --y;
G[x].push_back(y);
in>>C[x][y];
}
ofstream out( "maxflow.out" );
out<<Maximum_Flow();
return 0;
}