Cod sursa(job #1650562)

Utilizator DysKodeTurturica Razvan DysKode Data 11 martie 2016 19:03:46
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include <fstream>
#include <queue>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

queue <int> Q;
vector <int> G[1010],destSource;
int Cap[1010][1010],i,j,n,m,flow,c,x,y,arb[1010],r;

int HaveFl0w()
{
    Q.push( 1 );
    memset( arb , 0 , sizeof( arb ) );
    while( Q.size() )
    {
        for( vector<int>::iterator it = G[ Q.front() ].begin() ; it != G[ Q.front() ].end() ; it++ )
        {
            if( arb[ *it ] == 0 && Cap[ Q.front() ][ *it ] > 0 && *it != 1 )
            {
                arb[ *it ] = Q.front();
                Q.push( *it );
            }
        }
        Q.pop();
    }
    return arb[ n ];
}

int g3tCap( int x )
{
    int ans = Cap[ x ][ n ];
    while( arb[ x ] != 0 )
    {
        ans = min( ans , Cap[ arb[ x ] ][ x ] );
        x = arb[ x ];
    }
    return ans;
}

void s3tCap( int x, int c )
{
    Cap[ x ][ n ] -= c;
    while( arb[ x ] != 0 )
    {
        Cap[ arb[ x ] ][ x ] -= c;
        Cap[ x ][ arb[ x ] ] += c;
        x = arb[ x ];
    }
}

int main()
{
    fin>>n>>m;
    for( i = 1 ; i <= m ; i++ )
    {
        fin>>x>>y>>c;
        Cap[ x ][ y ] = c;
        G[ x ].push_back( y );
        G[ y ].push_back( x );
        if( y == n )
            destSource.push_back( x );
    }

    while( HaveFl0w() )
    {
        for( vector<int>::iterator it  = destSource.begin() ; it != destSource.end() ; it++ )
        {
            if( arb[ *it ] != 0 )
            {
                c = g3tCap( *it );
                if( c > 0 )
                {
                    flow += c;
                    s3tCap( *it , c );
                }
            }
        }
    }
    fout<<flow;

return 0;
}