传送门:点击打开链接
题意:一个地图有很多个地雷,如果走到距离这个地雷的曼哈顿距离<=k,那么这颗地雷就会爆炸。
现在要从左上角走到右下角,问最少需要拆除多少颗地雷。
刚开始一直在考虑从左上到右下去。。其实我们可以换一个角度去思考问题。
刚开始的时候,左下边界和右上边界肯定是用雷连通的。后来拆了一些雷之后,就变得不连通了。
如果我们能想到这里,就会惊讶的发现:最小割
然后接下来又会遇到2个问题,
第一个是如何去判断两个地雷是否连通,这个对两个地雷的位置认真讨论一下,要做到能O1的判断才行,不然在这里就可能会超时。
第二个是网络流是允许环的,这个我以前一直没有理解清楚。
只有费用流才不允许有负环,网络流有多少环都是可以的!
顺便看到了一个大神的dinic模板,感觉封装的真666,(然后扣出来保存了2333
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;
const int MX = 1e3;
const int MS = 4e5 + 5;
const int INF = 0x3f3f3f3f;
template<class T>
struct Max_Flow {
int n;
int Q[MX], sign;
int head[MX], level[MX], cur[MX], pre[MX];
int nxt[MS], pnt[MS], E;
T cap[MS];
void Init(int n) {
E = 0;
this->n = n + 1;
fill(head, head + this->n, -1);
}
void Add(int from, int to, T c, T rw = 0) {
pnt[E] = to; cap[E] = c; nxt[E] = head[from]; head[from] = E++;
pnt[E] = from; cap[E] = rw; nxt[E] = head[to]; head[to] = E++;
}
bool BFS(int s, int t) {
sign = t;
std::fill(level, level + n, -1);
int *front = Q, *tail = Q;
*tail++ = t; level[t] = 0;
while (front < tail && level[s] == -1) {
int u = *front++;
for (int e = head[u]; e != -1; e = nxt[e]) {
if (cap[e ^ 1] > 0 && level[pnt[e]] < 0) {
level[pnt[e]] = level[u] + 1;
*tail++ = pnt[e];
}
}
}
return level[s] != -1;
}
void Push(int t, T &flow) {
T mi = INF;
int p = pre[t];
for (int p = pre[t]; p != -1; p = pre[pnt[p ^ 1]]) {
mi = std::min(mi, cap[p]);
}
for (int p = pre[t]; p != -1; p = pre[pnt[p ^ 1]]) {
cap[p] -= mi;
if (!cap[p]) {
sign = pnt[p ^ 1];
}
cap[p ^ 1] += mi;
}
flow += mi;
}
void DFS(int u, int t, T &flow) {
if (u == t) {
Push(t, flow);
return;
}
for (int &e = cur[u]; e != -1; e = nxt[e]) {
if (cap[e] > 0 && level[u] - 1 == level[pnt[e]]) {
pre[pnt[e]] = e;
DFS(pnt[e], t, flow);
if (level[sign] > level[u]) {
return;
}
sign = t;
}
}
}
T Dinic(int s, int t) {
pre[s] = -1;
T flow = 0;
while (BFS(s, t)) {
std::copy(head, head + n, cur);
DFS(s, t, flow);
}
return flow;
}
};
Max_Flow<int>F;
char s[MX];
int T, n, m, k;
PII P[MX]; int tot;
int dist(PII a, PII b) {
return abs(a.first - b.first) + abs(a.second - b.second);
}
inline bool check(PII a, PII b, int k) {
if((a.first == b.first || a.second == b.second) && dist(a, b) <= 2 * k + 1) return 1;
if((a.first != b.first && a.second != b.second) && dist(a, b) <= 2 * k + 2) return 1;
return 0;
}
int main() {
//FIN;
scanf("%d", &T);
while(T--) {
tot = 0;
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= n; i++) {
scanf("%s", s + 1);
for(int j = 1; j <= m; j++) {
if(s[j] == '*') P[++tot] = PII(i, j);
}
}
int s = 0, t = 2 * tot + 1;
F.Init(t + 1);
for(int i = 1; i <= tot; i++) {
F.Add(i, i + tot, 1);
if(P[i].first + k >= n || P[i].second - k <= 1) {
F.Add(s, i, INF);
}
if(P[i].first - k <= 1 || P[i].second + k >= m) {
F.Add(i + tot, t, INF);
}
for(int j = i + 1; j <= tot; j++) {
if(check(P[i], P[j], k)) {
F.Add(i + tot, j, INF);
F.Add(j + tot, i, INF);
}
}
}
printf("%d\n", F.Dinic(s, t));
}
return 0;
}