Cod sursa(job #1656206)

Utilizator wilversingsMarius Aiordachioaei wilversings Data 18 martie 2016 22:04:14
Problema Flux maxim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <bitset>
#include <algorithm>
using namespace std;

const int NMAX = 1002;

int pumped[NMAX][NMAX], capacity[NMAX][NMAX], path[NMAX], N;
vector<int> graph[NMAX], fathers_of_N;
bitset<NMAX> check;
queue<int> Q;

bool BFS () {

    check.reset();

    Q.push (1);
    while (!Q.empty ()) {
        int node;
        node = Q.front ();
        check[node] = true;
        Q.pop ();

        if (node == N)
            continue;

        for (auto i : graph[node])
            if (!check[i] && capacity[node][i] - pumped[node][i] > 0) {
                path[i] = node;
                Q.push(i);
            }
    }

    return check[N];

}

int main () {

    freopen ("maxflow.in", "r", stdin);
    freopen ("maxflow.out", "w", stdout);

    int M, ans = 0;

    scanf ("%d%d", &N, &M);

    while (M--) {
        int a, b, f;

        scanf ("%d%d%d", &a, &b, &f);
        graph[a].push_back(b);
        graph[b].push_back(a);
        capacity[a][b] = f;

    }

    bool stop;
    while (BFS ()) {
        for (auto i : graph[N]) {
            if (pumped[i][N] == capacity[i][N]) continue;
            int _min = 2e9;
            int node = N;
            while (node != 1) {
                _min = min (_min, capacity[path[node]][node] - pumped[path[node]][node]);
                node = path[node];
            }
            ans += _min;
            node = N;
            while (node != 1) {
                pumped[path[node]][node] += _min;
                pumped[node][path[node]] -= _min;
                node = path[node];
            }
            stop = false;
        }
    }

    printf ("%d ", ans);

}