Cod sursa(job #2577871)

Utilizator ezioconnorVlad - Gabriel Iftimescu ezioconnor Data 9 martie 2020 23:20:58
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>

#define pii pair <int, int>
#define x first
#define y second

using namespace std;

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

int n, m, it = 1;
int viz[1001];
int level[1001], cap[1001][1001], flow[1001][1001];
vector <int> g[1001], g2[1001];

inline bool BFS() {
    queue <int> q;
    q.push(1);
    level[1] = 1;
    viz[1] = it;
    while (!q.empty()) {
        int nod = q.front();
        q.pop();
        for (auto vecin : g[nod]) {
            if (viz[vecin] != it && flow[nod][vecin] < cap[nod][vecin]) {
                level[vecin] = level[nod] + 1;
                viz[vecin] = it;
                q.push(vecin);
            }
        }
    }
    return (viz[n] == it);
}

inline int DFS(int nod, int val) {
    if (nod == n)
        return val;
    int s = 0;
    for (auto vecin : g[nod]) {
        if (level[vecin] == level[nod] + 1 && flow[nod][vecin] < cap[nod][vecin]) {
            int pump = DFS(vecin, min(val, cap[nod][vecin] - flow[nod][vecin]));
            flow[nod][vecin] += pump;
            flow[vecin][nod] -= pump;
            val -= pump;
            s += pump;
        }
    }
    return s;
}

int main() {
    in >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int x, y, c;
        in >> x >> y >> c;
        cap[x][y] = c;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    int rez = 0;
    while (BFS()) {
        ++it;
        rez += DFS(1, 2e9);
    }
    return out << rez, 0;
}