Cod sursa(job #777572)

Utilizator contdetestareTester contdetestare Data 12 august 2012 18:56:28
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <deque>
#define N 1010
#define INF 999999999
#define M 5010

using namespace std;

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

int n,i,j,m,x,y,c,C[N][N],F[N][N],PR[N],ANS=0,fmin=0;
vector <int> A[N];
bool V[N];
deque<int> Q;

bool BFS(){
    int nod;
    memset(V,0,sizeof(V));
    Q.clear();
    Q.push_back(1);V[1]=1;
    while (!Q.empty()) {
        nod=Q.front();Q.pop_front();
        for (int j=0;j<A[nod].size();j++)
            if (!V[A[nod][j]] && C[nod][A[nod][j]]<F[nod][A[nod][j]]) {
                V[A[nod][j]]=1;
                PR[A[nod][j]]=nod;
                if (A[nod][j]!=n) Q.push_back(A[nod][j]);
            }
    }
    return V[n];
}

int main() {
    f >> n >> m;
    for (i=1;i<=m;i++) {
        f >> x >> y >> c;
        C[x][y]=c;
        A[x].push_back(y);
        A[y].push_back(x);
    }
   for (ANS=0;BFS();)
       for (i=0;i<A[n].size();i++) {
            int nod=A[n][i];
            if (C[nod][n]==F[nod][n] || !V[nod]) continue;
            PR[n]=nod;
            fmin=INF;
            for (j=n;j>1;j=PR[j]) fmin=min(fmin,C[PR[j]][j]-F[PR[j]][j]);
            if (fmin==0) continue;
            for (j=n;j>1;j=PR[j]) F[PR[j]][j]+=fmin,F[j][PR[j]]-=fmin;
            ANS+=fmin;
       }
    g << ANS << '\n';
    f.close();g.close();
    return 0;
}