Pagini recente » Cod sursa (job #3193282) | Cod sursa (job #3294266) | Cod sursa (job #1186928) | Cod sursa (job #1367135) | Cod sursa (job #492260)
Cod sursa(job #492260)
/*
* File: main.cpp
* Author: bitone
*
* Created on October 13, 2010, 10:01 PM
*/
#include <queue>
#include <fstream>
#include <cstdlib>
#define MAX_N 1011
#define oo 1000000000
using namespace std;
typedef pair< int, int > pr;
/*
*
*/
int N;
vector< int > G[MAX_N];
int f[MAX_N];
int C[MAX_N][MAX_N], F[MAX_N][MAX_N];
inline bool BFS( void )
{
int x;
queue< int > Q;
vector< int >::const_iterator it, iend;
fill( f+2, f+N+1, -1 );
for( Q.push(1); !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] > F[x][*it] )
{
f[*it]=x;
Q.push(*it);
}
}
f[1]=0;
return -1 != f[N];
}
int main(int argc, char** argv)
{
int M, x, y, c, s;
ifstream in( "maxflow.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
in>>C[x][y];
}
for( s=0; BFS(); )
{
for( x=1; x <= N; ++x )
if( C[x][N] > F[x][N] )
{
c=C[x][N]-F[x][N];
for( y=x; f[y]; y=f[y] )
c=min( c, C[f[y]][y]-F[f[y]][y] );
s+=c;
F[x][N]+=c;
F[N][x]-=c;
for( y=x; f[y]; y=f[y] )
F[y][f[y]]-=c, F[f[y]][y]+=c;
}
}
ofstream out( "maxflow.out" );
out<<s<<'\n';
return EXIT_SUCCESS;
}