Cod sursa(job #794023)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 5 octombrie 2012 00:34:10
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>
#define LE1 1065
#define LE 5050
#define inf 1<<30
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int father[LE],i,n,m,cost[LE1][LE1],a,b,C,flux[LE1][LE1],result,viz[LE],col,Fmax;
void bfs(int nod)
{
    viz[nod]=true;
    int i;
    for(i=1; i<=n; ++i)
        if (cost[nod][i]&&viz[i]==false&&flux[nod][i]<cost[nod][i])
        {
            father[i]=nod;
            bfs(i);
        }
}
int main()
{
    f>>n>>m;

    for(i=1; i<=m; ++i)
    {
        f>>a>>b>>C;
        cost[a][b]=C;
    }

    while (true)
    {
        for(i=1; i<=n; ++i)
        {
            father[i]=0;
            viz[i]=0;
        }
        bfs(1);

        if (father[n]==0) break;

        int Nod=n;
        Fmax=inf;

        while (father[Nod]!=0)
        {
            Fmax=min(cost[father[Nod]][Nod]-flux[father[Nod]][Nod],Fmax);
            Nod=father[Nod];
        }

        Nod=n;

        while (father[Nod]!=0)
        {
            flux[father[Nod]][Nod]+=Fmax;
            Nod=father[Nod];
        }
        result+=Fmax;
    }

g<<result<<'\n';

    f.close();
    g.close();
    return 0;
}