Pagini recente » Cod sursa (job #1176338) | Cod sursa (job #1343417) | Cod sursa (job #2796302) | Cod sursa (job #761312) | Cod sursa (job #2402391)
#include <bits/stdc++.h>
#define pb push_back
using namespace std ;
const int NR = 1001 , oo = ( 1 << 30 ) ;
ifstream in ("maxflow.in") ;
ofstream out ("maxflow.out") ;
vector < int > v [ NR ] ;
int n , m , c [ NR ][ NR ] , ans ;
int father [ NR ] ;
bool bfs () {
int nod , i , flow ;
vector < int > :: iterator it ;
queue < int > q ;
q.push( 1 ) ;
father [ 1 ] = 1 ;
for ( i = 2 ; i <= n ; ++ i ) father [ i ] = 0 ;
while ( !q.empty() ) {
nod = q.front() ;
q.pop() ;
for ( it = v [ nod ].begin() ; it != v [ nod ].end() ; ++ it )
if ( !father [ *it ] && c [ nod ][ *it ] ) {
father [ *it ] = nod ;
q.push( *it ) ;
}
}
if ( !father [ n ] ) return 0 ;
for ( it = v [ n ].begin() ; it != v [ n ].end() ; ++ it ) {
nod = *it ;
flow = c [ *it ][ n ] ;
for ( ; nod != father [ nod ] && flow ; nod = father [ nod ] )
flow = min ( flow , c [ father [ nod ] ][ nod ] ) ;
if ( !flow ) continue ;
c [ *it ][ n ] -= flow ;
c [ n ][ *it ] += flow ;
nod = *it ;
for ( ; nod != father [ nod ] ; nod = father [ nod ] )
c [ father [ nod ] ][ nod ] -= flow ,
c [ nod ][ father [ nod ] ] += flow ;
ans += flow ;
}
return 1 ;
}
int main () {
int i , x , y , z ;
in >> n >> m ;
while ( m -- ) {
in >> x >> y >> z ;
c [ x ][ y ] = z ;
v [ x ].pb ( y ) ;
v [ y ].pb ( x ) ;
}
while ( bfs() ) ;
out << ans ;
}