Pagini recente » Cod sursa (job #2423335) | Cod sursa (job #3032573) | Cod sursa (job #2941979) | Cod sursa (job #2423088) | Cod sursa (job #1268407)
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 1e3 + 2;
int F[Nmax][Nmax];
int C[Nmax][Nmax];
vector <int> G[Nmax];
int coada[Nmax];
int tata[Nmax];
int N, M;
bool BFS( int S, int T )
{
memset( tata, 0, sizeof( tata ) );
int st = 1, dr = 1;
coada[st] = S;
while ( st <= dr )
{
int nod = coada[ st++ ];
for ( auto x: G[nod] )
if ( !tata[x] && F[nod][x] < C[nod][x] )
{
coada[ ++dr ] = x;
tata[x] = nod;
if ( x == T )
return 1;
}
}
return 0;
}
int flux( int S, int T )
{
int fl = 0, fmi;
while ( BFS( S, T ) )
{
for ( auto x: G[T] )
{
if ( !tata[x] || F[x][T] >= C[x][T] ) continue;
fmi = 1e9;
tata[T] = x;
for ( int nod = T; nod != S; nod = tata[nod] )
fmi = min( fmi, C[ tata[nod] ][nod] - F[ tata[nod] ][nod] );
fl += fmi;
if ( !fmi ) continue;
for ( int nod = T; nod != S; nod = tata[nod] )
{
F[ tata[nod] ][nod] += fmi;
F[nod][ tata[nod] ] -= fmi;
}
}
}
return fl;
}
void add_edge( int i, int j, int c )
{
G[i].push_back( j );
if ( c )
C[i][j] = c;
}
int main()
{
ifstream in("maxflow.in");
ofstream out("maxflow.out");
in >> N >> M;
for ( int i = 1, a, b, c; i <= M; ++i )
{
in >> a >> b >> c;
add_edge( a, b, c );
add_edge( b, a, 0 );
}
out << flux( 1, N ) << "\n";
return 0;
}