Cod sursa(job #280862)
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
#define INPUT "maxflow.in"
#define OUTPUT "maxflow.out"
#define CL(X) memset(X, 0, sizeof(X))
const int NMAX = 1001;
const long INFI = 2000000000;
ifstream fin(INPUT);
ofstream fout(OUTPUT);
int N, M;
long Cap[ NMAX ][ NMAX ], Flow[ NMAX ][ NMAX ];
int Father[ NMAX ], vis[ NMAX ], bf[ NMAX ];
void readData()
{
int Node1, Node2;
long Cost;
fin >> N >> M;
for(int i = 0; i < M; ++i)
{
fin >> Node1 >> Node2 >> Cost;
Cap[ Node1 ][ Node2 ] = Cost;
}
}
int BFS()
{
CL( vis );
vis[ 1 ] = 1;
bf[ 1 ] = 1;
Father[ 1 ] = -1;
long Poz = 1;
long Cont = 1;
while(Poz <= Cont)
{
long T = bf[ Poz ];
for(int i = 1; i <= N; ++i)
if(Cap[ T ][ i ] - Flow[ T ][ i ] > 0 && !vis[ i ])
bf[ ++Cont ] = i, vis[ i ] = 1, Father[ i ] = T;
++Poz;
}
return vis[ N ];
}
void solve()
{
long minim;
long flow = 0;
long T;
while( BFS() )
{
minim = INFI;
for(int j = 1; j <= N; ++j)
if(Cap[ j ][ N ] - Flow[ j ][ N ] > 0)
{
for(int i = N; Father[ i ] != -1; i = Father[ i ])
if(minim > Cap[ Father[ i ] ][ i ] - Flow[ Father[ i ] ][ i ]) minim = Cap[ Father[ i ] ][ i ] - Flow[ Father[ i ] ][ i ];
for(int i = N; Father[ i ] != -1; i = Father[ i ])
{
T = Father[ i ];
Flow[ T ][ i ] += minim;
Flow[ i ][ T ] -= minim;
if(i == N) flow += minim;
}
}
}
fout << flow << "\n";
}
int main()
{
readData();
solve();
fin.close();
fout.close();
return 0;
}