This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/6/GRL_6_A"
#include "../../graph/dinic.hpp"
#include <bits/stdc++.h>
using namespace std;
int main() {
ll v, e;
cin >> v >> e;
dinic<ll_dinic> dinic(v);
while (e--) {
ll u, v, c;
cin >> u >> v >> c;
dinic.add_edge(u, v, c);
}
ll ans = dinic.flow(0, v - 1);
cout << ans << endl;
}
#line 1 "test/onlinejudge.u-aizu.ac.jp/Maximum_Flow.0.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/6/GRL_6_A"
#line 2 "graph/dinic.hpp"
#line 2 "template.hpp"
#include <bits/stdc++.h>
using namespace std;
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define uniq(a) (a).erase(unique(all(a)), (a).end())
#define t0 first
#define t1 second
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
constexpr double pi = 3.14159265358979323846;
constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr ll sign(ll a) { return (a > 0) - (a < 0); }
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); }
constexpr ll pw(ll n) { return 1ll << n; }
constexpr ll flg(ll n) { return 63 - __builtin_clzll(n); }
constexpr ll clg(ll n) { return n == 1 ? 0 : flg(n - 1) + 1; }
constexpr ll safemod(ll x, ll mod) { return (x % mod + mod) % mod; }
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T> constexpr T sq(const T &a) { return a * a; }
template <typename T, typename U> constexpr bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <typename T, typename U> constexpr bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
os << "(" << a.first << ", " << a.second << ")";
return os;
}
template <typename T, typename U, typename V> ostream &operator<<(ostream &os, const tuple<T, U, V> &a) {
os << "(" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
os << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
os << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
os << ")";
return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
os << ")";
return os;
}
#ifdef ONLINE_JUDGE
#define dump(...) (void(0))
#else
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head &&head, Tail &&... tail) {
cerr << head;
if (sizeof...(Tail)) cerr << ", ";
debug(tail...);
}
#define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__)
#endif
struct rep {
struct itr {
ll v;
itr(ll v) : v(v) {}
void operator++() { ++v; }
ll operator*() const { return v; }
bool operator!=(itr i) const { return v < *i; }
};
ll l, r;
rep(ll l, ll r) : l(l), r(r) {}
rep(ll r) : rep(0, r) {}
itr begin() const { return l; };
itr end() const { return r; };
};
struct per {
struct itr {
ll v;
itr(ll v) : v(v) {}
void operator++() { --v; }
ll operator*() const { return v; }
bool operator!=(itr i) const { return v > *i; }
};
ll l, r;
per(ll l, ll r) : l(l), r(r) {}
per(ll r) : per(0, r) {}
itr begin() const { return r - 1; };
itr end() const { return l - 1; };
};
struct io_setup {
static constexpr int PREC = 20;
io_setup() {
cout << fixed << setprecision(PREC);
cerr << fixed << setprecision(PREC);
};
} iOS;
#line 4 "graph/dinic.hpp"
template <typename S> struct dinic {
using C = typename S::C;
struct _edge {
ll to, rev;
C cap;
};
struct edge {
ll from, to;
C cap, flow;
friend ostream &operator<<(ostream &os, const edge &e) {
return os << "(from: " << e.from << ", to: " << e.to << ", cap: " << e.cap << ", flow: " << e.flow << ")";
}
};
vector<vector<_edge>> g;
vector<ll> level, iter;
vector<pair<ll, ll>> pos;
dinic(ll n) : g(n), level(n), iter(n) {}
ll add_edge(ll from, ll to, C cap) {
ll from_id = g[from].size();
ll to_id = g[to].size();
if (from == to) ++to_id;
g[from].push_back({to, to_id, cap});
g[to].push_back({from, from_id, S::zero()});
pos.emplace_back(from, from_id);
return pos.size() - 1;
}
void change_edge(ll i, C new_cap, C new_flow) {
_edge &e = g[pos[i].first][pos[i].second], &re = g[e.to][e.rev];
e.cap = new_cap - new_flow;
re.cap = new_flow;
}
C flow(ll s, ll t, C lim = S::inf()) {
auto bfs = [&](ll s) -> void {
fill(level.begin(), level.end(), -1);
queue<ll> q;
level[s] = 0;
q.push(s);
while (!q.empty()) {
ll v = q.front();
q.pop();
for (_edge &e : g[v]) {
if (e.cap == S::zero() || level[e.to] >= 0) continue;
level[e.to] = level[v] + 1;
q.push(e.to);
}
}
};
auto dfs = [&](auto dfs, ll v, ll t, C lim) -> C {
if (v == t) return lim;
for (ll &i = iter[v]; i < (ll)g[v].size(); ++i) {
_edge &e = g[v][i];
if (level[v] >= level[e.to] || e.cap == S::zero()) continue;
C d = dfs(dfs, e.to, t, lim > e.cap ? e.cap : lim);
if (d == S::zero()) continue;
e.cap -= d;
g[e.to][e.rev].cap += d;
return d;
}
return S::zero();
};
C ret = S::zero();
while (true) {
bfs(s);
if (level[t] < 0 || lim == S::zero()) return ret;
fill(iter.begin(), iter.end(), 0);
C f;
while ((f = dfs(dfs, s, t, lim)) != S::zero()) {
ret += f;
lim -= f;
}
}
}
edge get_edge(ll i) const {
_edge e = g[pos[i].first][pos[i].second], re = g[e.to][e.rev];
return {pos[i].first, e.to, e.cap + re.cap, re.cap};
}
vector<edge> edges() const {
vector<edge> ret(pos.size());
for (ll i : rep(pos.size())) ret[i] = get_edge(i);
return ret;
}
vector<bool> cut(ll s) const {
vector<bool> ret(g.size());
auto dfs = [&](auto dfs, ll v) -> void {
if (ret[v]) return;
ret[v] = true;
for (_edge e : g[v]) {
if (e.cap) dfs(dfs, e.to);
}
};
dfs(dfs, s);
return ret;
}
};
struct ll_dinic {
using C = ll;
static C zero() { return 0; }
static C inf() { return numeric_limits<C>::max(); }
};
#line 3 "test/onlinejudge.u-aizu.ac.jp/Maximum_Flow.0.test.cpp"
#line 5 "test/onlinejudge.u-aizu.ac.jp/Maximum_Flow.0.test.cpp"
using namespace std;
int main() {
ll v, e;
cin >> v >> e;
dinic<ll_dinic> dinic(v);
while (e--) {
ll u, v, c;
cin >> u >> v >> c;
dinic.add_edge(u, v, c);
}
ll ans = dinic.flow(0, v - 1);
cout << ans << endl;
}