Cod sursa(job #1449507)

Utilizator xtreme77Patrick Sava xtreme77 Data 9 iunie 2015 20:58:01
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.54 kb
/**
 * Code by Patrick Sava
 * "Spiru Haret" National College of Bucharest
 **/

# include "fstream"
# include "cstring"
# include "vector"
# include "queue"
# include "bitset"
# include "algorithm"
# include "map"
# include "unordered_map"
# include "deque"
# include "string"
# include "iomanip"
# include "cmath"
# include "stack"
# include "cassert"

const char IN [ ] =  "fmcm.in" ;
const char OUT [ ] = "fmcm.out" ;
const int MAX = 500 ;

# define pb push_back
# define mp make_pair
# define FORN( a , b , c ) for ( int a = b ; a <= c ; ++ a )
# define FORNBACK( a , b , c ) for ( int a = b ; a >= c ; -- a )

using namespace std ;

ifstream cin ( IN ) ;
ofstream cout ( OUT ) ;

int cap [ MAX ] [ MAX ] , flux [ MAX ] [ MAX ] ;
int dist [ MAX ] , tata [ MAX ] ;

vector < pair < int , int > > gr [ MAX ] ;

bitset < MAX > IQ ;

queue < int > Q ;

int local_flox , S , D ;

inline int flow ( )
{
    IQ.reset() ;
    memset ( dist , 0x3f , sizeof ( dist ) ) ;
    Q.push ( S ) ;
    IQ [ S ] = 1 ;
    tata [ S ] = 0 ;
    dist [ S ] = 0 ;

    while ( !Q.empty() )
    {
        int nod = Q.front() ;
        Q.pop() ;
        IQ [ nod ] = 0 ;

        for ( auto x : gr [ nod ] )
        {
            if ( cap [ nod ] [ x.first ] > flux [ nod ] [ x.first ] and dist [ x.first ] > dist [ nod ] + x.second )
            {
                dist [ x.first ] = dist [ nod ] + x.second ;
                tata [ x.first ] = nod ;
                if ( IQ [ x.first ] == 0 )
                {
                    Q.push ( x.first ) ;
                    IQ [ x.first ] = 1 ;
                }
            }
        }
    }

    if ( dist [ D ] != 0x3f )
    {
         local_flox = 1 << 30 ;

        for ( int i = D ; tata [ i ] ; i = tata [ i ] )
        {
             local_flox = min (  local_flox , cap [ tata [ i ] ] [ i ] - flux [ tata [ i ] ] [ i ] ) ;
        }

        for ( int i = D ; tata [ i ] ; i = tata [ i ] )
        {
            flux [ tata [ i ] ] [ i ] +=  local_flox ;
            flux [ i ] [ tata [ i ] ] -=  local_flox ;
        }
    }

    return local_flox != 0 ;
}

int main ( void )
{
    int n , m ;
    cin >> n >> m >> S >> D ;
    while ( m -- )
    {
        int x , y , c , z ;
        cin >> x >> y >> c >> z ;
        cap [ x ] [ y ] = c ;
        gr [ x ].pb ( { y ,  z } ) ;
        gr [ y ].pb ( { x , -z } ) ;
    }
    int CET = 0 ;

    while ( flow ( ) )
        CET += dist [ D ] * local_flox ;

    cout << CET << '\n' ;
    return 0;
}