Cod sursa(job #1109280)

Utilizator cata_popescuPopescu Catalina cata_popescu Data 16 februarie 2014 21:54:55
Problema Flux maxim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
//
//  main.cpp
//  Dinic
//
//  Created by Catalina Popescu on 2/16/14.
//  Copyright (c) 2014 Catalina Popescu. All rights reserved.
//

#include <fstream>
using namespace std;
ifstream g1("maxflow.in");
ofstream g2("maxflow.out");
int n,m,s,d,c[101][101],f[101][101],t[101],flux,v[101];
int BF(int s, int d)
{
    int p,u,x,i;
    for(i=1;i<=n;i++) t[i]=0;
    p=u=1;v[p]=s;t[s]=-1;
    while(p<=u)
    {
        x=v[p];
        for(i=1;i<=n;i++)
        {
            if(!t[i]&&c[x][i]>f[x][i])
            {
                v[++u]=i;
                t[i]=x;
                if(i==d) return 1;
            }
        }
        p++;
    }
    return 0;
}

void dinic()
{
    int i,j,r;
    while(BF(s,d))
    {
        for(i=1;i<=n;i++)
        {
            if(t[i]==-1||c[i][d]<=f[i][d]) continue;
            r=c[i][d]-f[i][d];
            j=i;
            while(j!=s && j)
            {
                r=min(r,c[t[j]][j]-f[t[j]][j]);
                j=t[j];
            }
            if(r==0) continue;
            f[i][d]+=r;
            f[d][i]-=r;
            j=i;
            while(j!=s)
            {
                f[t[j]][j]+=r;
                f[j][t[j]]-=r;
                j=t[j];
            }
            flux+=r;
        }
    }
    
}
int main()
{
    int x,y,z,i;
    g1>>n>>m;
    s=1;d=n;
    for(i=1;i<=m;i++)
    {
        g1>>x>>y>>z;
        c[x][y]=z;
    }
    dinic();
    g2<<flux<<'\n';
    return 0;
}