Cod sursa(job #1514013)

Utilizator Vlad_lsc2008Lungu Vlad Vlad_lsc2008 Data 30 octombrie 2015 14:40:09
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb
#include <cstdio>
#include <iostream>
#include <stack>
#include <cstring>
#include <vector>
#define pb push_back
#define nmax 1005
#define inf 0x7fffffff
#define minim(a,b) ((a)<(b)? (a):(b))
using namespace std;

int n,m1,c[nmax][nmax],f[nmax][nmax],padre[nmax],u[nmax];
vector<int> m[nmax];
stack<int> s;

int lant(int nod)
{
    //cout<<nod<<' ';
    if(nod==n) return 1;
    u[nod]=1;
    for(int i=0;i<m[nod].size();i++)
        if(!u[m[nod][i]] && c[nod][m[nod][i]]>f[nod][m[nod][i]])
        {
            padre[m[nod][i]]=nod;
            if(lant(m[nod][i])) return 1;
        }
    return 0;
}

int main()
{
    int n1,n2,cost,i,nod,maxflow=0,newflow;
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    scanf("%d%d",&n,&m1);
    for(;m1;m1--)
    {
        scanf("%d%d%d",&n1,&n2,&cost);
        m[n1].pb(n2); m[n2].pb(n1);
        c[n1][n2]=cost;
    }
    while(lant(1))
    {
        lant(1);
        nod=n;
        newflow=inf;
        memset(u,0,sizeof(u));
       // cout<<'\n';
        while(nod!=1)
        {
            newflow=minim(newflow,c[padre[nod]][nod]-f[padre[nod]][nod]);
            //cout<<nod<<' '<<newflow<<"   ";
            nod=padre[nod];
        }
        nod=n;
        while(nod!=1)
        {
            f[padre[nod]][nod]+=newflow;
            f[nod][padre[nod]]-=newflow;
            nod=padre[nod];
        }
        maxflow+=newflow;
        //cout<<maxflow<<'\n';
    }
    printf("%d\n",maxflow);
    fclose(stdin);
    fclose(stdout);
    return 0;
}