Cod sursa(job #1896679)

Utilizator filip.mihalutMihalut Filip filip.mihalut Data 28 februarie 2017 20:33:54
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,tata[1010],i,j,m,flow[1010][1010],ans,c,D,S,x,y;
vector< int > G[1010];
queue< int > Q;

bool bf()
{
    for( int i = 1 ; i <= n ; i++ )
        tata[ i ] = 0;
    tata[ S ] = S;
    Q.push( S );
    while( Q.size() )
    {
        for( auto it : G[ Q.front() ] )
        {
            if( flow[ Q.front() ][ it ] && tata[ it ] == 0 )
            {
                tata[ it ] = Q.front();
                Q.push( it );
            }
        }

        Q.pop();
    }
    return tata[ D ];
}

void addflow( int nod )
{
    if( tata[ nod ] == nod )
        return;
    c = min( c , flow[ tata[ nod ] ][ nod ] );
    addflow( tata[ nod ] );
    flow[ nod ][ tata[ nod ] ] += c;
    flow[ tata[ nod ] ][ nod ] -= c;
}

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

    S = 1;
    D = n;

    while( bf() )
    {
        for( auto it : G[ D ] )
        {
            if( tata[ it ] && flow[ it ][ D ] )
            {
                c = flow[ it ][ D ];
                addflow( it );
                flow[ it ][ D ] -= c;
                flow[ D ][ it ] += c;
                ans += c;
            }
        }
    }
    fout<<ans;
return 0;
}