1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include <bits/stdc++.h>
using namespace std; const int N = 10005, M = 200005, inf = 1e9; int n, m, S, T; int h[N], e[M], ne[M], w[M], idx; int d[N], cur[N];
void add (int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++; }
bool bfs () { queue <int> q; memset (d, -1, sizeof d); q.push (S), d[S] = 0, cur[S] = h[S]; while (!q.empty ()) { int t = q.front(); q.pop();
for (int i = h[t]; ~i; i = ne[i]) { int j = e[i]; if (d[j] != -1 || w[i] == 0) continue; d[j] = d[t] + 1; cur[j] = h[j]; if (j == T) return true; q.push (j); } } return false; }
int find (int u, int limit) { if (u == T) return limit; int flow = 0; for (int i = cur[u]; ~i && flow < limit; i = ne[i]) { cur[u] = i; int j = e[i]; if (d[j] != d[u] + 1 || w[i] == 0) continue; int t = find (j, min (w[i], limit - flow)); if (t == 0) d[j] = -1; w[i] -= t, w[i^1] += t, flow += t; } return flow; }
int dinic () { int r = 0, flow; while (bfs ()) while (flow = find (S, inf)) r += flow; return r; }
int main () { cin >> n >> m >> S >> T; memset (h, -1, sizeof h); while (m --) { int a, b, c; cin >> a >> b >> c; add (a, b, c), add (b, a, 0); } cout << dinic () << endl; }
|