Cod sursa(job #1254905)

Utilizator rebound212Mihnea Savu rebound212 Data 3 noiembrie 2014 18:14:58
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <algorithm>
#include <cstdio>
#include <cstring>
#define MP make_pair
#define PB push_back
#define W 1001
using namespace std;
int  c[W][W],f[W][W],i,n,m,t[W],q[W],p,u,nod,fmin,flow,x,y,cap;
inline bool BF()
{
    memset(t,0,sizeof(t));
    p=u=0;
    q[p]=1;
    while(p<=u)
    {
        nod=q[p];
        for(i=1; i<=n; i++)
        {
            if(!t[i] && c[nod][i]>f[nod][i])
            {
                t[i]=nod;
                q[++u]=i;
                if(i==n) return 1;
            }

        }
        p++;
    }
    return 0;

}
int main()
{
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(i=1; i<=m; i++)
    {
        scanf("%d %d %d",&x,&y,&cap);
        c[x][y]=cap;
    }
    flow=0;
    while( BF())
    {
        fmin=1000000000;
        i=n;
        while(i!=1)
        {
            fmin=min(fmin,c[t[i]][i]-f[t[i]][i]);
            i=t[i];
        }
        i=n;
        while(i!=1)
        {
            f[t[i]][i]+=fmin;
            f[i][t[i]]-=fmin;
            i=t[i];
        }
        flow+=fmin;
    }
    printf("%d",flow);

    return 0;
}