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
| #include <climits> #include <queue> #include <algorithm> const int MAXN = 40; struct ; struct Node { Edge *e, *curr; int level; } N[MAXN * MAXN * MAXN + 2]; struct { Node *u, *v; Edge *next, *rev; int cap, flow; Edge(Node *u, Node *v, int cap) : u(u), v(v), cap(cap), flow(0), next(u->e) {} }; void addEdge(int u, int v, int cap) { N[u].e = new Edge(&N[u], &N[v], cap); N[v].e = new Edge(&N[v], &N[u], 0); N[u].e->rev = N[v].e; N[v].e->rev = N[u].e; } namespace Dinic { bool makeLevelGraph(Node *s, Node *t, int n) { for (int i = 0; i < n; i++) N[i].level = 0; std::queue<Node *> q; q.push(s); s->level = 1; while (!q.empty()) { Node *u = q.front(); q.pop(); for (Edge *e = u->e; e; e 大专栏 [BZOJ 3144][HNOI 2013] 切糕= e->next) { if (e->cap > e->flow && e->v->level == 0) { e->v->level = u->level + 1; if (e->v == t) return true; q.push(e->v); } } } return false; } int findPath(Node *s, Node *t, int limit = INT_MAX) { if (s == t) return limit; for (Edge *&e = s->curr; e; e = e->next) { if (e->cap > e->flow && e->v->level == s->level + 1) { int flow = findPath(e->v, t, std::min(limit, e->cap - e->flow)); if (flow > 0) { e->flow += flow; e->rev->flow -= flow; return flow; } } } return 0; } int solve(int s, int t, int n) { int res = 0; while (makeLevelGraph(&N[s], &N[t], n)) { for (int i = 0; i < n; i++) N[i].curr = N[i].e; int flow; while ((flow = findPath(&N[s], &N[t])) > 0) res += flow; } return res; } } int p, q, r; int getID(int x, int y, int z) { if (z == 0) return 0; return (z - 1) * p * q + (x - 1) * q + y; } bool valid(int x, int y) { return (x > 0) && (y > 0) && (x <= p) && (y <= q); } int main() { int D; scanf("%d %d %d %d", &p, &q, &r, &D); static int v[MAXN + 1][MAXN + 1][MAXN + 1]; for (int k = 1; k <= r; k++) for (int i = 1; i <= p; i++) for (int j = 1; j <= q; j++) scanf("%d", &v[i][j][k]); const int s = 0, t = p * q * r + 1; const int d[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} }; for (int i = 1; i <= p; i++) for (int j = 1; j <= q; j++) { for (int k = 1; k <= r; k++) { addEdge(getID(i, j, k - 1), getID(i, j, k), v[i][j][k]); if (k > D) for (int l = 0; l < 4; l++) { int x = i + d[l][0], y = j + d[l][1]; if (valid(x, y)) addEdge(getID(i, j, k), getID(x, y, k - D), INT_MAX); } } addEdge(getID(i, j, r), t, INT_MAX); } printf("%dn", Dinic::solve(s, t, t + 1)); return 0; }
|