Cod sursa(job #2257577)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 10 octombrie 2018 10:47:38
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 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];
    int tata[1010];
    int cmin;

    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) {
        if (viz[s])
            return false;
        viz[s] = 1;
        if (s == t)
            return true;
        for (auto i : adia[s]) {
            if (edges[i].cap && go(edges[i].to, t)) {
                cmin = min(cmin, edges[i].cap);
                tata[edges[i].to] = i;
                return true;
            }
        }
        return false;
    }
    int max_flow(int s, int t) {
        int f = 0;
        while (1) {
            cmin = 1e9;
            memset(viz, 0, sizeof viz);
            if (go(s, t)) {
                f += cmin;
                for (int i = t; i != s; i = edges[tata[i]].from)
                    edges[tata[i]].cap -= cmin, edges[tata[i] ^ 1].cap += cmin;
            }
            else
                break;
        }
        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;
}