firiexp's Library

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

View the Project on GitHub firiexp/library

:heavy_check_mark: test/aoj_grl_6_a_maxflow_lower_bound.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

#include "../graph/maxflow_lower_bound.cpp"

int main() {
    int n, m;
    cin >> n >> m;
    MaxFlowLowerBound<ll> g(n);
    for(int i = 0; i < m; ++i) {
        int u, v;
        ll c;
        cin >> u >> v >> c;
        g.add_edge(u, v, 0, c);
    }
    auto res = g.max_flow(0, n - 1);
    if(!res.first) {
        cout << -1 << '\n';
        return 0;
    }
    cout << res.second << '\n';
    return 0;
}
#line 1 "test/aoj_grl_6_a_maxflow_lower_bound.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

#line 1 "flow/dinic.cpp"
template<class T, bool directed>
class Dinic {
    void bfs(int s){
        fill(level.begin(),level.end(), -1);
        queue<int> Q;
        level[s] = 0;
        Q.emplace(s);
        while(!Q.empty()){
            int v = Q.front(); Q.pop();
            for (auto &&e : G[v]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[v] + 1;
                    Q.emplace(e.to);
                }
            }
        }
    }
 
    T dfs(int v, int t, T f){
        if(v == t) return f;
        for(int &i = iter[v]; i < G[v].size(); i++){
            edge &e = G[v][i];
            if(e.cap > 0 && level[v] < level[e.to]){
                T d = dfs(e.to, t, min(f,  e.cap));
                if(d == 0) continue;
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
        return 0;
    }
public:
    struct edge {
        int to{}; T cap; int rev{};
        edge() = default;
        edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {}
    };
 
    vector<vector<edge>> G;
    vector<int> level, iter;
    Dinic() = default;
    explicit Dinic(int n) : G(n), level(n), iter(n) {}
 
    void add_edge(int from, int to, T cap){
        G[from].emplace_back(to, cap, G[to].size());
        G[to].emplace_back(from, directed ? 0 : cap,  G[from].size()-1);
    }
 
 
    T flow(int s, int t, T lim = INF<T>){
        T ret = 0;
        while(true) {
            bfs(s);
            if(level[t] < 0 || lim == 0) break;
            fill(iter.begin(),iter.end(), 0);
            while(true){
                T f = dfs(s, t, lim);
                if(f == 0) break;
                ret += f;
                lim -= f;
            }
        }
        return ret;
    }
};

/**
 * @brief Dinic法(Dinic)
 */
#line 2 "graph/maxflow_lower_bound.cpp"

template<class T>
class MaxFlowLowerBound {

    struct raw_edge {
        int from{}, to{};
        T lower{}, upper{};
    };

public:
    int n;
    vector<raw_edge> edges;
    MaxFlowLowerBound() = default;
    explicit MaxFlowLowerBound(int n) : n(n) {}

    void add_edge(int from, int to, T lower, T upper) {
        edges.push_back({from, to, lower, upper});
    }

    pair<bool, T> max_flow(int s, int t) {
        int ss = n, tt = n + 1;
        Dinic<T, true> mf(n + 2);
        vector<T> b(n, 0);
        auto add_edge = [&](int from, int to, T cap) {
            int idx = (int)mf.G[from].size();
            mf.add_edge(from, to, cap);
            return pair<int, int>{from, idx};
        };

        for(auto &&e : edges) {
            mf.add_edge(e.from, e.to, e.upper - e.lower);
            b[e.from] -= e.lower;
            b[e.to] += e.lower;
        }

        auto ts = add_edge(t, s, INF<T>);
        T req = 0;
        vector<pair<int, int>> super_edges;
        for(int v = 0; v < n; ++v) {
            if(b[v] > 0) {
                req += b[v];
                super_edges.emplace_back(add_edge(ss, v, b[v]));
            } else if(b[v] < 0) {
                mf.add_edge(v, tt, -b[v]);
            }
        }

        if(mf.flow(ss, tt) != req) return {false, 0};

        for(auto &&id : super_edges) {
            if(mf.G[id.first][id.second].cap != 0) return {false, 0};
        }

        int to = mf.G[ts.first][ts.second].to;
        int rev = mf.G[ts.first][ts.second].rev;
        T base = mf.G[to][rev].cap;
        mf.G[ts.first][ts.second].cap = 0;
        mf.G[to][rev].cap = 0;

        T add = mf.flow(s, t);
        return {true, base + add};
    }
};

/**
 * @brief 下限制約付きs-t最大流 (Max Flow with Lower Bounds)
 */
#line 21 "test/aoj_grl_6_a_maxflow_lower_bound.test.cpp"

int main() {
    int n, m;
    cin >> n >> m;
    MaxFlowLowerBound<ll> g(n);
    for(int i = 0; i < m; ++i) {
        int u, v;
        ll c;
        cin >> u >> v >> c;
        g.add_edge(u, v, 0, c);
    }
    auto res = g.max_flow(0, n - 1);
    if(!res.first) {
        cout << -1 << '\n';
        return 0;
    }
    cout << res.second << '\n';
    return 0;
}
Back to top page