program_contest_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ferin-15/program_contest_library

:heavy_check_mark: test/GRL6A_2.test.cpp

Back to top page

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/6/GRL_6_A"
#include "../memo/macro.hpp"
#include "../graph/dinic_with_cost_scalling.cpp"

signed main(void) {
    ll v, e;
    cin >> v >> e;
    dinicWithCostScalling<ll> flow(v);
    REP(i, e) {
        ll u, v, c;
        cin >> u >> v >> c;
        flow.add_edge(u, v, c);
    }

    cout << flow.maxflow(0, v-1) << endl;

    return 0;
}

#line 1 "test/GRL6A_2.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/6/GRL_6_A"
#line 1 "memo/macro.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
const ll INF = 1LL<<60;
#line 1 "graph/dinic_with_cost_scalling.cpp"
// BEGIN CUT
template<class T>
class dinicWithCostScalling {
    struct edge{
        int to;
        T cap;
        int rev;
        bool isrev;
    };

    vector<vector<edge>> G;
    vector<int> level, iter;

    void bfs(int s, const T base) {
        level.assign(G.size(), -1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while(que.size()) {
            int v = que.front(); que.pop();
            for(auto i: G[v]) {
                if(i.cap >= base && level[i.to] < 0) {
                    level[i.to] = level[v] + 1;
                    que.push(i.to);
                }
            }
        }
    }
    T dfs(int v, const int t, T f, const T base) {
        if(v == t) return f;
        for(int &i = iter[v]; i<(ll)G[v].size(); ++i) {
            edge &e = G[v][i];
            if(e.cap >= base && level[v] < level[e.to]) {
                T d = dfs(e.to, t, min(f, e.cap), base);
                if(d > 0) {
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

public:
    dinicWithCostScalling() {}
    dinicWithCostScalling(int n) : G(n), level(n), iter(n) {}

    void add_edge(int from, int to, T cap) {
        G[from].push_back({to, cap, (int)G[to].size(), false});
        G[to].push_back({from, 0, (int)G[from].size()-1, true});
    }

    // max_capは辺の最大容量
    T maxflow(int s, int t, T max_cap = 1LL<<30) {
        T flow = 0;
        const int tmp = __builtin_ctzll(max_cap);
        for(T base = 1LL<<tmp; base >= 1;) {
            bfs(s, base);
            if(level[t] < 0) {
                base >>= 1;
                continue;
            }
            iter.assign(G.size(), 0);
            flow += dfs(s, t, 1LL<<60, base);
        }
        return flow;
    }

    friend ostream &operator <<(ostream& out, const dinicWithCostScalling& a){
        out << endl;
        for(int i = 0; i < (int)a.G.size(); i++) {
            for(auto &e : a.G[i]) {
                if(e.isrev) continue;
                auto &rev_e = a.G[e.to][e.rev];
                out << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
            }
        }
        return out;
    }
};
// END CUT
#line 4 "test/GRL6A_2.test.cpp"

signed main(void) {
    ll v, e;
    cin >> v >> e;
    dinicWithCostScalling<ll> flow(v);
    REP(i, e) {
        ll u, v, c;
        cin >> u >> v >> c;
        flow.add_edge(u, v, c);
    }

    cout << flow.maxflow(0, v-1) << endl;

    return 0;
}

Back to top page