Cod sursa(job #754543)

Utilizator Theorytheo .c Theory Data 2 iunie 2012 15:38:05
Problema Flux maxim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include<fstream>
#include<stdlib.h>
#include<vector>
#define nmax 1002
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int C[nmax][nmax];//capacitatea
int F[nmax][nmax];
int TT[nmax];
vector <int> G[nmax];
int N,M;
int cd[nmax];
bool viz[nmax];
int BF()
{
    for(int i = 1; i <= N; i++, viz[i] = 0 );
    cd[0] = cd[1] = 1;
    viz[1] = 1;
    for(int i = 1; i <= cd[0]; i++)
    {
        int nod = cd[i];
        if(nod == N) continue;
        for(int j = 0 ;j < G[nod].size(); j++)
        {
            int V = G[nod][j];
            if(C[nod][V] == F[nod][V] || viz[V]) continue;

            TT[V] = nod;
            cd[++cd[0]] = V;
            viz[V] = true;
        }
    }
    return viz[N] ;
}
int solve()
{
    int flow ;
    for(flow = 0 ; BF(); )
        for(int i = 0 ;i < G[N].size(); i++)
        {
           int nod = G[N][i] ;
           if(F[nod][N] == C[nod][N] || !viz[nod]) continue;

           TT[N] = nod;
           int fluxmin = 10000000;
           for(nod  = N; nod != 1; nod = TT[nod] )
                fluxmin = min (fluxmin, C[TT[nod]][nod] - F[TT[nod]][nod]);

           for(nod = N; nod != 1; nod = TT[nod])
                F[TT[nod]][nod] += fluxmin;

           flow += fluxmin;

        }
        return flow;
}
void read()
{
    fin >>N>> M;
    int x, y, z;
    for(int i = 1; i <= M; ++i)
       {
           fin >> x>> y >>z;
           C[x][y] += z;
           G[x].push_back(y);
           G[y].push_back(x);
        }
}
int main()
{
    read();
        fout << solve();
    fin.close();
    return 0;
}