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
| const int MAXN = 2e3 + 5; const int INF = 0x3f3f3f3f;
struct Edge{ int to,val,nxt; }e[MAXN];
int head[MAXN], deep[MAXN]; int n, m, s, t, cnt; ll res;
inline void init() { for (int i = 1; i <= n; i++) head[i] = -1; cnt = 0; }
inline void add(int from, int to, int val) { e[cnt] = Edge{to, val, head[from]}; head[from] = cnt++; }
inline int bfs() { for (int i = 2; i <= t; i++) deep[i] = -1; queue<int>q; deep[s] = 1; q.push(s); while (!q.empty()) { int front = q.front(); q.pop(); for (int i = head[front]; i != -1; i = e[i].nxt) { if (deep[e[i].to] == -1 && e[i].val) { deep[e[i].to] = deep[front] + 1; q.push(e[i].to); } } } return (deep[t] != -1); }
inline int dfs(int x, int totFlow) { if (x == t || totFlow ==0) return totFlow; int ans = totFlow; for (int i = head[x]; i != -1; i = e[i].nxt) { if (deep[e[i].to] == deep[x] + 1 && e[i].val) { int flow = dfs(e[i].to, min(totFlow,e[i].val)); e[i].val -= flow; e[i ^ 1].val += flow; totFlow -= flow; if (totFlow == 0) return ans; } } return ans - totFlow; }
inline void dinic() { while (bfs()) { res += dfs(s, INF); } }
|