From 5f9b1033c7d4ce5ffe4aaa9e000496aca2ad2a1b Mon Sep 17 00:00:00 2001 From: Baoshuo Ren Date: Sat, 12 Mar 2022 08:54:56 +0800 Subject: [PATCH] =?UTF-8?q?2172.=20Dinic/ISAP=E6=B1=82=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/11826004/ --- AcWing/2172/2172.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 AcWing/2172/2172.cpp diff --git a/AcWing/2172/2172.cpp b/AcWing/2172/2172.cpp new file mode 100644 index 00000000..7eeea751 --- /dev/null +++ b/AcWing/2172/2172.cpp @@ -0,0 +1,86 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +int n, m, s, t, flow, ans; + +const int N = 10005, + M = 100005; + +// Graph +int idx, head[N], edge[M << 1], ver[M << 1], next[M << 1]; + +void add(int u, int v, int w) { + next[idx] = head[u]; + edge[idx] = w; + ver[idx] = v; + head[u] = idx++; +} + +// Dinic +int d[N], cur[N]; + +bool bfs() { + memset(d, 0x00, sizeof(d)); + std::queue q; + q.push(s); + d[s] = 1; + cur[s] = head[s]; + while (!q.empty()) { + int u = q.front(); + q.pop(); + for (int i = head[u]; ~i; i = next[i]) { + int v = ver[i], + w = edge[i]; + if (w && !d[v]) { + d[v] = d[u] + 1; + cur[v] = head[v]; + if (v == t) return true; + q.push(v); + } + } + } + return false; +} + +int dinic(int u, int limit) { + if (u == t) return limit; + + int flow = 0; + for (int i = cur[u]; ~i && flow < limit; i = next[i]) { + cur[u] = i; + + int v = ver[i]; + if (d[v] == d[u] + 1 && edge[i]) { + int k = dinic(v, std::min(edge[i], limit - flow)); + if (!k) d[v] = 0; + edge[i] -= k; + edge[i ^ 1] += k; + flow += k; + } + } + return flow; +} + +int main() { + std::ios::sync_with_stdio(false); + memset(head, 0xff, sizeof(head)); + cin >> n >> m >> s >> t; + for (int i = 1; i <= m; i++) { + int u, v, w; + cin >> u >> v >> w; + add(u, v, w); + add(v, u, 0); + } + + while (bfs()) { + while (flow = dinic(s, 0x3f3f3f3f)) ans += flow; + } + + cout << ans << endl; + return 0; +}