Cod sursa(job #932592)

Utilizator paul_gabryelPaul Buda paul_gabryel Data 29 martie 2013 01:09:50
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb

#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;

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

const int N = 1001;

vector<int> G[N];
int n,m,C[N][N],T[N],SOL;
queue<int> Q;

void READ ()
{

    in>>n>>m;

    for( int x,y,c ; m ; --m )
    {
        in>>x>>y;

        in>>C[x][y];

        G[x].push_back(y);
        G[y].push_back(x);

    }

}

int BFS ()
{

    memset(T,0,sizeof(T));

    T[1]=1;

    for( Q.push(1) ; Q.size() ; Q.pop() )
    {

        int nod = Q.front();

        for( vector<int>::iterator it=G[nod].begin() ; it < G[nod].end() ; ++it)
            if( C[nod][*it] && !T[*it] )
            {
                T[*it]=nod;
                Q.push(*it);
            }

    }

    return T[n];

}

void SOLVE ()
{

    for( int flow ; BFS () ; )
    {

        int nod=n;
        flow=C[T[n]][n];

        for( ; nod != 1 ; nod=T[nod] )
            if( flow > C[T[nod]][nod] )
                flow=C[T[nod]][nod];

        for( nod = n ; nod != 1 ; nod=T[nod] )
        {

            C[T[nod]][nod]-=flow;
            C[nod][T[nod]]+=flow;

        }

        SOL+=flow;

    }

}

void PRINT ()
{

    out<<SOL;

}

int main ()
{

    READ ();
    SOLVE ();
    PRINT ();

    return 0;

}