This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ei1333/library
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A" #include "../../template/template.hpp" #include "../../graph/flow/dinic-capacity-scaling.hpp" int main() { int V, E; scanf("%d %d", &V, &E); DinicCapacityScaling< int > g(V); for(int i = 0; i < E; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); g.add_edge(a, b, c); } printf("%d\n", g.max_flow(0, V - 1)); }
#line 1 "test/verify/aoj-grl-6-a-4.test.cpp" #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A" #line 1 "template/template.hpp" #include<bits/stdc++.h> using namespace std; using int64 = long long; const int mod = 1e9 + 7; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 >& p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { explicit FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } #line 4 "test/verify/aoj-grl-6-a-4.test.cpp" #line 1 "graph/flow/dinic-capacity-scaling.hpp" /** * @brief Dinic Capacity Scaling(最大流) * @docs docs/dinic-capacity-scaling.md */ template< typename flow_t > struct DinicCapacityScaling { static_assert(is_integral< flow_t >::value, "template parameter flow_t must be integral type"); const flow_t INF; struct edge { int to; flow_t cap; int rev; bool isrev; int idx; }; vector< vector< edge > > graph; vector< int > min_cost, iter; flow_t max_cap; explicit DinicCapacityScaling(int V) : INF(numeric_limits< flow_t >::max()), graph(V), max_cap(0) {} void add_edge(int from, int to, flow_t cap, int idx = -1) { max_cap = max(max_cap, cap); graph[from].emplace_back((edge) {to, cap, (int) graph[to].size(), false, idx}); graph[to].emplace_back((edge) {from, 0, (int) graph[from].size() - 1, true, idx}); } bool build_augment_path(int s, int t, const flow_t &base) { min_cost.assign(graph.size(), -1); queue< int > que; min_cost[s] = 0; que.push(s); while(!que.empty() && min_cost[t] == -1) { int p = que.front(); que.pop(); for(auto &e : graph[p]) { if(e.cap >= base && min_cost[e.to] == -1) { min_cost[e.to] = min_cost[p] + 1; que.push(e.to); } } } return min_cost[t] != -1; } flow_t find_augment_path(int idx, const int t, flow_t base, flow_t flow) { if(idx == t) return flow; flow_t sum = 0; for(int &i = iter[idx]; i < (int)graph[idx].size(); i++) { edge &e = graph[idx][i]; if(e.cap >= base && min_cost[idx] < min_cost[e.to]) { flow_t d = find_augment_path(e.to, t, base, min(flow - sum, e.cap)); if(d > 0) { e.cap -= d; graph[e.to][e.rev].cap += d; sum += d; if(flow - sum < base) break; } } } return sum; } flow_t max_flow(int s, int t) { if(max_cap == flow_t(0)) return flow_t(0); flow_t flow = 0; for(int i = 63 - __builtin_clzll(max_cap); i >= 0; i--) { flow_t now = flow_t(1) << i; while(build_augment_path(s, t, now)) { iter.assign(graph.size(), 0); flow += find_augment_path(s, t, now, INF); } } return flow; } void output() { for(int i = 0; i < graph.size(); i++) { for(auto &e : graph[i]) { if(e.isrev) continue; auto &rev_e = graph[e.to][e.rev]; cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl; } } } }; #line 6 "test/verify/aoj-grl-6-a-4.test.cpp" int main() { int V, E; scanf("%d %d", &V, &E); DinicCapacityScaling< int > g(V); for(int i = 0; i < E; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); g.add_edge(a, b, c); } printf("%d\n", g.max_flow(0, V - 1)); }