[SCOI 2007] 蜥蜴

题目大意

n×mn \times m 的网格上有一些柱子,一些柱子上有蜥蜴,蜥蜴可以跳向曼哈顿距离小于等于 dd 的柱子上,一个柱子上最多有一只蜥蜴,当蜥蜴跳走后,原柱子高度减一。求无法跳出网格的蜥蜴数量的最小值。

1n,  m201 \leqslant n, \; m \leqslant 20

1d41 \leqslant d \leqslant 4

题目链接

【SCOI 2007】蜥蜴 - Luogu 2472

题解

建图跑网络流,最大流为最多跳出去的蜥蜴只数。

每个点拆成两个点,记为 11 号点和 22 号点。

对于柱子高度的限制,从对应的 11 号点向 22 号点连一条容量为高度的边。

对与每一个点,从它的 22 号点向与其曼哈顿距离小于等于 dd 的点的 11 号点连一条容量无限的边。

最后,从源点向每一只蜥蜴的 11 号点连一条容量为 11 的边,从能跳出去的 22 号点向汇点连一条容量为11的边。

代码

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
100
101
102
103
104
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <queue>
#include <algorithm>
// #define DBG
const int MAXN = 20;
struct Edge;
struct Node {
Edge *e, *curr;
int level;
} N[MAXN * MAXN * 2 + 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) {
#ifdef DBG
printf("edge : %d --> %d, cap = %d\n", u, v, cap);
#endif
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;
}
struct 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 operator()(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;
}
} dinic;
int n, m;
int getID(int x, int y, int k) {
return k * n * m + (x - 1) * m + y;
}
int main() {
int d;
scanf("%d %d %d", &n, &m, &d);
const int s = 0, t = n * m * 2 + 1;
for (int i = 1; i <= n; i++) {
static char str[MAXN + 1];
scanf("%s", str + 1);
for (int j = 1; j <= m; j++) if (str[j] != '0')
addEdge(getID(i, j, 0), getID(i, j, 1), str[j] - '0');
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
static char str[MAXN + 1];
scanf("%s", str + 1);
for (int j = 1; j <= m; j++) if (str[j] == 'L') {
cnt++;
addEdge(s, getID(i, j, 0), 1);
}
}
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) {
for (int k = 1; k <= n; k++) for (int l = 1; l <= m; l++) {
if (abs(i - k) + abs(j - l) <= d)
addEdge(getID(i, j, 1), getID(k, l, 0), INT_MAX);
}
if (i <= d || i > n - d || j <= d || j > m - d)
addEdge(getID(i, j, 1), t, INT_MAX);
}
printf("%d\n", cnt - dinic(s, t, t + 1));
return 0;
}