Pagini recente » Cod sursa (job #2940387) | Cod sursa (job #1423311) | Cod sursa (job #1349804) | Cod sursa (job #3248686) | Cod sursa (job #2769306)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1005;
ifstream fin( "maxflow.in" );
ofstream fout( "maxflow.out" );
int N, M;
vector <int> ad[NMAX];
int flow[NMAX][NMAX];
int cap[NMAX][NMAX];
int pred[NMAX];
int _MAXFLOW;
void Read() {
fin >> N >> M;
int x, y, z;
for( int i = 1; i <= M; ++i ) {
fin >> x >> y >> z;
ad[x].push_back( y );
cap[x][y] = z;
}
}
void FindAugPath() {
for( int i = 1; i <= N; ++i )
pred[i] = 0;
deque <int> Q;
Q.push_back( 1 );
while( !Q.empty() ) {
int x = Q.front();
Q.pop_front();
for( int i = 0; i < ad[x].size(); ++i ) {
int w = ad[x][i];
if( cap[x][w] - flow[x][w] > 0 && !pred[w] ) {
pred[w] = x;
Q.push_back( w );
}
}
}
}
void AugPath() {
int maxf = 2000000000;
for( int i = N; i != 1; i = pred[i] ) {
int p = pred[i];
maxf = min( maxf, cap[p][i] - flow[p][i] );
}
for( int i = N; i != 1; i = pred[i] ) {
int p = pred[i];
flow[p][i] += maxf;
}
_MAXFLOW += maxf;
}
void Do() {
bool done = false;
while( !done ) {
FindAugPath();
if( pred[N] == 0 ) done = true;
else
AugPath();
}
fout << _MAXFLOW;
}
int main()
{
Read();
Do();
return 0;
}