Cod sursa(job #1872943)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 8 februarie 2017 18:12:06
Problema Flux maxim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
#include <vector>
#define Nmax 1001
#define inf 1e9
using namespace std;

ifstream f("maxflow.in");
ofstream g("maxflow.out");

int n,m,rez,flw[Nmax][Nmax];
bool uz[Nmax];
vector<int> V[Nmax];


int dfs(int nod,int mn)
{
    int cns = 0;
    if (nod==n)
    {
        rez+=mn;
        return mn;
    }
    for (int i=0;i<V[nod].size();i++)
    {
        if (flw[nod][V[nod][i]]!=0 && !uz[V[nod][i]])
        {
            int x;
            uz[V[nod][i]] = 1;
            x = dfs(V[nod][i],min(mn-cns,flw[nod][V[nod][i]]));
            uz[V[nod][i]] = 0;
            flw[nod][V[nod][i]] -= x;
            flw[V[nod][i]][nod] += x;
            cns+=x;
        }
    }
    return cns;
}

int main()
{
    f>>n>>m;
    for (int i=1;i<=m;i++)
    {
        int x,y;
        f>>x>>y;
        f>>flw[x][y];
        V[x].push_back(y);
        V[y].push_back(x);
    }

    uz[1] = 1;
    dfs(1,inf);

    g<<rez;

    return 0;
}