Cod sursa(job #1872421)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 8 februarie 2017 11:09:30
Problema Flux maxim Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 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;
bool uz[Nmax];
struct ceva{
    int nod,val;
};
vector<ceva> 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 (V[nod][i].val!=0 && !uz[V[nod][i].nod])
        {
            int x;
            uz[V[nod][i].nod] = 1;
            x = dfs(V[nod][i].nod,min(mn-cns,V[nod][i].val));
            uz[V[nod][i].nod] = 0;
            V[nod][i].val -= x;
            cns+=x;
        }
    }
    return cns;
}

int main()
{
    f>>n>>m;
    for (int i=1;i<=m;i++)
    {
        int x;
        ceva crt;
        f>>x>>crt.nod>>crt.val;
        V[x].push_back(crt);
    }

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

    g<<rez;

    return 0;
}