[HNOI 2013] 切糕

题目大意

切糕是 p×q×rp \times q \times r 的长方体,每个点有一个违和感 vx,y,zv_{x, y, z}。先要水平切开切糕(即对于每个纵轴,切面与其有且只有一个交点),要求水平上相邻两点的切面高度差小于等于 DD,求切面违和感和的最小值。

1p,  q,  r401 \leqslant p, \; q, \; r \leqslant 40

0v1,0000 \leqslant v \leqslant 1,000

题目链接

【HNOI 2013】切糕 - LibreOJ 2384

题解

最小割。

用边连接相邻两个高度的的点,边 (x,y,z1)(x,y,z)(x, y, z - 1) \rightarrow (x, y, z) 容量为 vx,y,zv_{x, y, z},由源点发散出边连接第一层的每个点,最后一层的点收缩在汇点,这是没有DD的限制是的答案。连接所有形如 (x,y,z)(x,y,zD)(x, y, z) \rightarrow (x, y, z - D) 的边,这样,当水平相邻的两个点切面差大于 DD 时,最小割的图会由这样的边连在一起而没有被隔开。

代码

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 <cstdio>
#include <climits>
#include <queue>
#include <algorithm>
const int MAXN = 40;
struct Edge;
struct Node {
Edge *e, *curr;
int level;
} N[MAXN * MAXN * MAXN + 2];
struct Edge {
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 = 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("%d\n", Dinic::solve(s, t, t + 1));
return 0;
}