Cod sursa(job #2257572)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 10 octombrie 2018 10:44:27
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>
using namespace std;

struct Edge {
    int from, to, cap;
};

namespace Flow {
    vector <Edge> edges;
    vector <int> adia[1010];
    bool viz[1010];

    void add_edge(int from, int to, int cap) {
        adia[from].push_back(edges.size());
        edges.push_back({ from, to, cap });
        adia[to].push_back(edges.size());
        edges.push_back({ to, from, 0 });
    }

    bool go(int s, int t, int c) {
        if (viz[s])
            return false;
        viz[s] = 1;
        if (s == t)
            return true;
        for (auto i : adia[s]) {
            if (edges[i].cap >= c && go(edges[i].to, t, c)) {
                edges[i].cap -= c;
                edges[i ^ 1].cap += c;
                return true;
            }
        }
        return false;
    }
    int max_flow(int s, int t) {
        int f = 0, c = 1e9;
        while (c) {
            memset(viz, 0, sizeof viz);
            if (go(s, t, c))
                f += c;
            else
                c /= 2;
        }
        return f;
    }
}

int main()
{
    ifstream in("maxflow.in");
    ofstream out("maxflow.out");
    int n, m, a, b, c;
    in >> n >> m;

    while (m--) {
        in >> a >> b >> c;
        Flow::add_edge(a, b, c);
    }

    out << Flow::max_flow(1, n) << '\n';

    return 0;
}