Cod sursa(job #1803459)

Utilizator BrandonChris Luntraru Brandon Data 11 noiembrie 2016 15:02:56
Problema Flux maxim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

#define Pe pair <int, int>
#define mp make_pair
#define fi first
#define se second
using namespace std;

const int MaxN = 1005, Inf = 0x3f3f3f3f;

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

int n, m, Ans;
int G[MaxN][MaxN], father[MaxN], Flow[MaxN][MaxN];
queue <int> Q;

inline void QClear() {
    while (Q.size()) {
        Q.pop();
    }
}

bool Bfs() {
    QClear();
    memset(father, 0, sizeof father);
    father[1] = -1;
    Q.push(1);
    while (Q.size()) {
        int node = Q.front();
        Q.pop();
        if (node == n) {
            return true;
        }

        for (int i = 1; i <= n; ++i) {
            if (!father[i] and G[node][i] - Flow[node][i]) {
                Q.push(i);
                father[i] = node;
            }
        }
    }

    return false;
}

int RoadCap(int node = n) {
    int ans = Inf;
    while (father[node] != -1) {
        int parent = father[node];
        ans = min(ans, G[parent][node] - Flow[parent][node]);
        node = parent;
    }

    return ans;
}

void Pump(int Quantity, int node = n) {
    while (father[node] != -1) {
        int parent = father[node];
        Flow[parent][node] += Quantity;
        Flow[node][parent] -= Quantity;
        node = parent;
    }
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        G[a][b] = c;
    }

    while (Bfs()) {
        int CurrCap = RoadCap();
        Pump(CurrCap);
    }

    for (int i = 2; i <= n; ++i) {
        if (!G[1][i]) {
            continue;
        }

        Ans += Flow[1][i];
    }

    cout << Ans << '\n';
    return 0;
}