Cod sursa(job #2972997)

Utilizator Iordache_CezarIordache Cezar Iordache_Cezar Data 30 ianuarie 2023 19:48:23
Problema Flux maxim Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>
#define NMAX 1008

using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");

int n, m, flow;
bool viz[NMAX];
map < pair <int, int>, int> h;
vector <int> G[NMAX];

int DFS(int nod, int minim);

int main()
{
    int x, y, z;
    fin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        fin >> x >> y >> z;
        G[x].push_back(y);
        h[{x, y}] = z;
        if (h[{y, x}])
            G[y].push_back(x);
    }
    x = 1;
    while (x)
    {
        for (int i = 0; i <= n; i++) viz[i] = 0;
        x = DFS(1, INT_MAX);
        flow += x;
    }
    fout << flow;
    return 0;
}

int DFS(int nod, int minim)
{
    viz[nod] = 1;
    if (nod == n)
        return minim;
    for (auto vf : G[nod])
        if (viz[vf] == 0 && h[{nod, vf}])
        {
            int x = DFS(vf, min(minim, h[{nod, vf}]));
            if (x > 0)
            {
                h[{nod, vf}] -= x;
                h[{vf, nod}] += x;
                return x;
            }
        }
    return 0;
}