Cod sursa(job #936771)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 8 aprilie 2013 17:24:47
Problema Flux maxim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
#define LE 1000
int flux[LE][LE],cap[LE][LE];
int i,j,result,color,n,m,xx,yy,nodf,node;
bool okz=true;
int viz[LE],father[LE],mflow;
#define inf 1<<30
void dfs(int nod)
{
    if (viz[nod]==color||okz==true)
        return;
    viz[nod]=color;

    if (nod==n)
    {
        okz=true;
        return;
    }
    for(int i=1; i<=n; ++i)
        if (cap[nod][i]-flux[nod][i]>0&&viz[i]!=color)
        {
            father[i]=nod;
            dfs(i);
        }
}
int main()
{
    f>>n>>m;
    for(i=1; i<=m; ++i)
    {
        f>>xx>>yy;
        f>>cap[xx][yy];
    }

    for (; okz==true;)
    {
        okz=false;
        ++color;
        dfs(1);
        if (okz==false) break;
        nodf=node=n;
        mflow=inf;

        while (node!=1)
        {
            mflow=min(mflow,cap[father[node]][node]-flux[father[node]][node]);
            node=father[node];
        }
        while (nodf!=1)
        {
            flux[father[nodf]][nodf]+=mflow;
            flux[nodf][father[nodf]]-=mflow;
            nodf=father[nodf];
        }
        result+=mflow;
    }

    g<<result<<'\n';

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