Pagini recente » Cod sursa (job #1611080) | Cod sursa (job #1107743) | Cod sursa (job #2037221) | Cod sursa (job #1042682) | Cod sursa (job #405779)
Cod sursa(job #405779)
#include <queue>
#include <vector>
#include <fstream>
/*
*
*/
using namespace std;
int N;
int C[1000][1000], d[1000];
vector< vector< int > > G;
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;
vector< int > father( N+1, -1 );
vector< int >::const_iterator it, iend;
Q.push( 0 );
d[0]=0x3f3f3f3f;
while( -1 == father[N] && !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] > 0 )
{
Q.push(*it);
father[*it]=x;
d[*it]=min( d[x], C[x][*it] );
}
}
if( -1 == father[N] )
return -1;
father[0]=-1;
for( x=N; -1 != father[x]; x=father[x] )
{
C[father[x]][x]-=d[N];
if( !C[x][father[x]] )
G[x].push_back( father[x] );
C[x][father[x]]+=d[N];
}
return d[N];
}
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;
for( ; M; --M )
{
in>>x>>y;
--x, --y;
in>>C[x][y];
G[x].push_back(y);
}
ofstream out( "maxflow.out" );
out<<Maximum_Flow();
return 0;
}