From 51e252392e82c9ff20cd468e6bf76efdbef00c41 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 26 Feb 2023 08:20:56 +0800 Subject: [PATCH] =?UTF-8?q?P3386=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E4=BA=8C=E5=88=86=E5=9B=BE=E6=9C=80=E5=A4=A7=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/103040235 --- Luogu/P3386/P3386.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Luogu/P3386/P3386.cpp b/Luogu/P3386/P3386.cpp index 7c8fe57d..db10d091 100644 --- a/Luogu/P3386/P3386.cpp +++ b/Luogu/P3386/P3386.cpp @@ -1,5 +1,6 @@ #include #include +#include #include using std::cin; @@ -10,19 +11,19 @@ const int N = 505, M = 5e4 + 5; const int INF = 0x3f3f3f3f; -int n, m, e, s, t, flow, ans; +int n, m, e, s, t; int idx, head[N << 1], ver[M << 2], edge[M << 2], next[M << 2]; int d[N << 1], cur[N << 1]; void add(int u, int v, int w) { - next[idx] = head[u]; ver[idx] = v; edge[idx] = w; + next[idx] = head[u]; head[u] = idx++; } bool bfs() { - std::fill_n(d, N << 1, 0); + std::fill(std::begin(d), std::end(d), 0); std::queue q; @@ -38,7 +39,7 @@ bool bfs() { int v = ver[i], w = edge[i]; - if (w && !d[v]) { + if (!d[v] && w) { d[v] = d[u] + 1; cur[v] = head[v]; if (v == t) return true; @@ -54,20 +55,21 @@ 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], w = edge[i]; - if (w && d[v] == d[u] + 1) { + if (d[v] == d[u] + 1 && w) { int k = dinic(v, std::min(limit - flow, w)); if (!k) d[v] = 0; + flow += k; edge[i] -= k; edge[i ^ 1] += k; - flow += k; } } @@ -78,7 +80,7 @@ int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); - std::fill_n(head, N << 1, -1); + std::fill(std::begin(head), std::end(head), -1); cin >> n >> m >> e; @@ -97,15 +99,17 @@ int main() { for (int i = 1, u, v; i <= e; i++) { cin >> u >> v; - add(u, n + v, 1); - add(n + v, u, 0); + add(u, v + n, 1); + add(v + n, u, 0); } + int res = 0; + while (bfs()) { - while (flow = dinic(s, INF)) ans += flow; + while (int flow = dinic(s, INF)) res += flow; } - cout << ans << endl; + cout << res << endl; return 0; }