Cod sursa(job #1074945)

Utilizator hevelebalazshevele balazs hevelebalazs Data 8 ianuarie 2014 10:43:48
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <stdio.h>
#include <vector>
#define fr(i,a,b) for(int i=a;i<b;++i)
#define p P[n]
#define N 1000
using namespace std;
vector<int>o[N];
int c[N][N],f[N][N];
int L[N],l;
int Q[N],q;
int P[N];
int v,e;
int main(){
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    scanf("%i%i",&v,&e);
    int x,y,z;
    fr(i,0,e){
        scanf("%i%i%i",&x,&y,&z);
        o[x-1].push_back(y-1);
        o[y-1].push_back(x-1);
        c[x-1][y-1]=z;
        }
    int F=0;
    while(1){
        q=0;
        Q[q++]=0;
        L[0]=++l;
        fr(i,0,q){
            int b=Q[i];
            int s=o[b].size();
            fr(j,0,s){
                int e=o[b][j];
                if(c[b][e]==f[b][e]||L[e]==l) continue;
                L[e]=l;
                Q[q++]=e;
                P[e]=b;
                if(e==v-1)break;
                }
            if(L[v-1]==l)break;
            }
        if(L[v-1]<l) break;
        int m=-1;
        for(int n=v-1;n;n=p) if(m==-1||m>c[p][n]-f[p][n]) m=c[p][n]-f[p][n];
        for(int n=v-1;n;n=p) f[p][n]+=m,f[n][p]-=m;
        F+=m;
        }
    printf("%i",F);
    return 0;
    }