Pagini recente » Cod sursa (job #806420) | Cod sursa (job #2953612) | Cod sursa (job #2755056) | Cod sursa (job #1435101) | Cod sursa (job #442316)
Cod sursa(job #442316)
#include <queue>
#include <cstdlib>
#include <fstream>
#define Nmax 1001
#define SIZE 8219
/*
*
*/
using namespace std;
ifstream in;
int ifile;
char file[SIZE];
int C[Nmax][Nmax];
vector< int > f, d;
vector< vector< int > > G;
vector< int >::const_iterator it, iend;
inline void read( int& x )
{
x=0;
while( file[ifile] < '0' || file[ifile] > '9' )
if( ++ifile == SIZE )
{
in.read( file, SIZE );
ifile=0;
}
while( file[ifile] >= '0' && file[ifile] <= '9' )
{
x=x*10+file[ifile]-'0';
if( ++ifile == SIZE )
{
in.read( file, SIZE );
ifile=0;
}
}
}
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;
in.open( "maxflow.in" );
read(N); read(M);
G.resize(N);
f.resize(N);
d.resize(N);
for( ; M; --M )
{
read(x); read(y);
--x, --y;
read( C[x][y] );
G[x].push_back(y);
G[y].push_back(x);
}
ofstream out( "maxflow.out" );
out<<MaximumFlow()<<'\n';
return EXIT_SUCCESS;
}