Cod sursa(job #1873175)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 8 februarie 2017 20:30:41
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>
using namespace std;

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

const int  NMAX = 1024,
            INF = 0x3f3f3f3f;

int ant;

vector<int> g[NMAX];
int flow[NMAX][NMAX],
     cap[NMAX][NMAX],
     far[NMAX],
     gws[NMAX];

bool pump(int src, int snk) {
    static int it = 0; ++it;

    deque<int> q;
    int u, now, res;

    gws[src] = it;
    q.push_back(src);
    while (!q.empty()) {
        u = q.front();
        q.pop_front();

        for (auto v: g[u]) if (gws[v] != it && cap[u][v] - flow[u][v] > 0) {
            q.push_back(v);
            gws[v] = it;
            far[v] = u; } }

    if (gws[snk] < it)
        return false;

    res = INF;
    for (now = snk; now != src; now = far[now])
        res = min(res, cap[far[now]][now] - flow[far[now]][now]);

    for (now = snk; now != src; now = far[now]) {
        flow[far[now]][now]+= res;
        flow[now][far[now]]-= res; }

    ant+= res;

    return true; }

int main(void) {
    int n, m, u, v, c;

    fi >> n >> m;
    for (int i = 0; i < m; ++i) {
        fi >> u >> v >> c;
        cap[u][v]+= c;
        g[u].push_back(v);
        g[v].push_back(u); }

    while (pump(1, n));

    fo << ant << '\n';

    return 0; }