Cod sursa(job #2402391)

Utilizator Andrei-27Arhire Andrei Andrei-27 Data 10 aprilie 2019 17:38:20
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#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 ;
}