From 3690e2a6f31975e2e8db536c2e37e6d9808acc37 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 18 Mar 2022 21:43:40 +0800 Subject: [PATCH] =?UTF-8?q?P1402=20=E9=85=92=E5=BA=97=E4=B9=8B=E7=8E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R71702515 --- Luogu/P1402/P1402.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 Luogu/P1402/P1402.cpp diff --git a/Luogu/P1402/P1402.cpp b/Luogu/P1402/P1402.cpp new file mode 100644 index 00000000..fd099da4 --- /dev/null +++ b/Luogu/P1402/P1402.cpp @@ -0,0 +1,119 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1005, + M = 10005; + +int n, f, d, flow, ans; + +// 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]; + ver[idx] = v; + edge[idx] = w; + head[u] = idx++; +} + +// Dinic +int dist[N], cur[N]; + +bool bfs() { + memset(dist, 0x00, sizeof(dist)); + std::queue q; + q.push(0); + dist[0] = 1; + cur[0] = head[0]; + + 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 && !dist[v]) { + dist[v] = dist[u] + 1; + cur[v] = head[v]; + if (v == n) return true; + q.push(v); + } + } + } + return false; +} + +int dinic(int u, int limit) { + if (u == n) return limit; + + int flow = 0; + for (int &i = cur[u]; ~i && flow < limit; i = next[i]) { + int v = ver[i], + w = edge[i]; + if (dist[v] == dist[u] + 1 && w) { + int k = dinic(v, std::min(w, limit - flow)); + if (!k) dist[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 >> f >> d; + + for (int i = 1; i <= f; i++) { + add(0, i, 1); + add(i, 0, 0); + } + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= f; j++) { + int x; + cin >> x; + if (x) { + add(j, f + d + i, 1); + add(f + d + i, j, 0); + } + } + } + + for (int i = 1; i <= n; i++) { + add(f + d + i, f + d + n + i, 1); + add(f + d + n + i, f + d + i, 0); + } + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= d; j++) { + int x; + cin >> x; + if (x) { + add(f + d + n + i, f + j, 1); + add(f + j, f + d + n + i, 0); + } + } + } + + n = f + d + n * 2 + 1; + for (int i = 1; i <= d; i++) { + add(f + i, n, 1); + add(n, f + i, 0); + } + + while (bfs()) { + while (flow = dinic(0, 0x3f3f3f3f)) ans += flow; + } + cout << ans << endl; + return 0; +}