Cod sursa(job #2695279)

Utilizator vladboss2323Ciorica Vlad vladboss2323 Data 12 ianuarie 2021 12:52:48
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.5 kb
#include <bits/stdc++.h>

using namespace std;

struct Graph
{
    int n, src, dst;
    // edge is pair (neighbor, color)
    // color == 0 ==> BLACK (graph edge)
    // color == 1 ==> RED   (solution edge)
    vector<vector<long long>> cap;
    vector<vector<int>> adj;

    Graph(int n) : n(n), adj(n),
        cap(n, vector<long long>(n, 0)) {}

    void AddEdge(int a, int b, int c)
    {
        adj[a].push_back(b);
        adj[b].push_back(a);
        cap[a][b] += c;
        // cap[b][a] == 0
    }

    vector<long long> ek_iter()
    {
        vector<bool> vis(n, false);
        vector<int> parent(n, -1);
        vector<long long>flows;
        vector<int> q;
        auto push = [&](int node, int par)
        {
            if (vis[node])
                return;
            vis[node] = true;
            parent[node] = par;
            q.push_back(node);
        };
        vector<int> dest_childs;
        push(src, -1);
        for (int i = 0; i < (int)q.size(); ++i)
        {
            int node = q[i];
            for (auto nei : adj[node])
            {
                if (cap[node][nei] > 0)
                {
                    //cout << node << " " << nei << endl;
                    if(nei == n - 1)
                        dest_childs.push_back(node);
                    push(nei, node);
                }
            }
        }
        /*for(auto x : q)
            cout << x << " ";
        cout << endl;
        */
        if (parent[dst] == -1)
            return flows;

        for(auto child : dest_childs)
        {
            parent[dst] = child;
            long long flow = 2e18;
            for (int node = dst; node != src; node = parent[node])
                flow = min(flow, cap[parent[node]][node]);
            flows.push_back(flow);
            assert(flow > 0);
        }
        /*for(int i = 0 ; i < n ; i++)
        {
            cout << i << " -> ";
            for(int j = 0 ; j < n ; j++)
                cout << cap[i][j] << " ";
            cout << '\n';
        }
        */
        for(int i = 0 ; i < dest_childs.size(); i++)
        {
            parent[dst] = dest_childs[i];
            for (int node = dst; node != src; node = parent[node])
            {
                //cout << node << " ";
                cap[parent[node]][node] -= flows[i];
                cap[node][parent[node]] += flows[i];
            }
        }
        /*cout << endl;
        for(int i = 0 ; i < n ; i++)
        {
            cout << i << " -> ";
            for(int j = 0 ; j < n ; j++)
                cout << cap[i][j] << " ";
            cout << '\n';
        }
        cout << endl;
        */
        return flows;
    }

    long long FordFulkerson(int src, int dst)
    {
        this->src = src;
        this->dst = dst;
        long long max_flow = 0;
        while (true)
        {
            vector<long long> curr_flow = ek_iter();
            if(curr_flow.size() == 0)
                break;
            for(auto flow : curr_flow)
                max_flow += flow;
        }
        return max_flow;
    }
};

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

    int n, m;
    cin >> n >> m;
    Graph G(n);
    for (int i = 0; i < m; ++i)
    {
        int a, b, c;
        cin >> a >> b >> c;
        --a;
        --b;
        G.AddEdge(a, b, c);
    }

    auto answer = G.FordFulkerson(0, n - 1);
    cout << answer << '\n';

    return 0;
}