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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <cstring> #define MAXN 1000002 #define INF 2000000000 int min(int a, int b) { if (a < b) return a; else return b; } struct Edge { int v, nx, w; }e[MAXN]; std::queue <int> q; int n, m, head[MAXN], ecnt = 1, x, y, z, r, k, dep[MAXN], cur[MAXN], qe, p; void add(int f, int t, int w) { e[++ecnt] = (Edge) {t, head[f] , w}; head[f] = ecnt; e[++ecnt] = (Edge) {f, head[t], 0}; head[t] = ecnt; } bool bfs(int s, int t) { memset(dep, 0x7f, sizeof(dep)); while (!q.empty()) q.pop(); for (int i = 1; i <= (n + n + qe + p + 2); i++) { cur[i] = head[i]; } dep[s] = 0; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (int i = head[v]; i; i = e[i].nx) { int to = e[i].v; if (dep[to] > INF && e[i].w) { dep[to] = dep[v] + 1; q.push(to); } } } if (dep[t] < INF) return 1; else return 0; } int dfs(int u, int t, int l) { if (!l || u == t) return l; int fl = 0 , f; for (int i = cur[u]; i; i = e[i].nx) { cur[u] = i; int to = e[i].v; if (dep[to] == dep[u] + 1 && (f = dfs(to, t, min(l, e[i].w)))) { fl += f; l -= f; e[i ^ 1].w += f; e[i].w -= f; if (!l) break; } } return fl; } int Dinic(int s, int t) { int maxf = 0; while (bfs(s, t)) { maxf += dfs(s, t, INF); } return maxf; } int main() { scanf("%d%d%d", &n, &p, &qe); r = n + n + p + qe + 1; k = n + n + p + qe + 2; for (int i = 1; i <= p; i++) { add(r, i, 1); } for (int i = 1; i <= qe; i++) { add(p + i, k, 1); } for (int i = 1; i <= n; i++) { add(i + p + qe, i + p + qe + n, 1); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= p; j++) { scanf("%d", &x); if (x == 1) { add(j, i + p + qe, 1); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= qe; j++) { scanf("%d", &x); if (x == 1) { add(i + p + qe + n, j + p, 1); } } } printf("%d\n", Dinic(r, k)); return 0; }
|