# BZOJ1066【SCOI2007】蜥蜴 <网络流>

【SCOI2007】蜥蜴

Description

Input

Output

Sample Input
5 8 2
00000000
02000000
00321100
02000000
00000000
........
........
..LLLL..
........
........
Sample Output
1

HINT
100%的数据满足：1<=r, c<=20, 1<=d<=4

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define MAX_N 20
#define INF 2147483647
using namespace std;
int n, m, r, s, t, cnt, tot, id[MAX_N+5][MAX_N+5], map[MAX_N*MAX_N*2+5][MAX_N*MAX_N*2+5];
char a[MAX_N+5][MAX_N+5], b[MAX_N+5][MAX_N+5];
void build(int x, int y) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if ((i != x || j != y) && (a[i][j] != '0') && r*r >= (x-i)*(x-i)+(y-j)*(y-j))
map[id[x][y]+1][id[i][j]] = INF;
}
int d[MAX_N*MAX_N*2+5];
bool BFS() {
queue <int> que;
memset(d, -1, sizeof(d));
d[s] = 0, que.push(s);
while (!que.empty()) {
int u = que.front();	que.pop();
for (int v = 0; v <= cnt; v++) {
if (d[v] != -1 || !map[u][v])	continue;
d[v] = d[u]+1, que.push(v);
}
}
return d[t] != -1;
}
int DFS(int u, int flow) {
if (u == t)	return flow;	int ret = 0;
for (int v = 0; v <= cnt; v++) {
if (d[v] != d[u]+1 || !map[u][v])	continue;
int tmp = DFS(v, min(flow, map[u][v]));
map[u][v] -= tmp, map[v][u] += tmp, flow -= tmp, ret += tmp;
if (!flow)	break;
}
if (!ret)	d[u] = -1;
return ret;
}
int Dinic() {
int ret = 0;
while (BFS())	ret += DFS(s, INF);
return ret;
}
int main() {
scanf("%d%d%d", &n, &m, &r);
for (int i = 1; i <= n; i++) {
scanf("%s", a[i]+1);
for (int j = 1; j <= m; j++) {
if (a[i][j] == '0')	continue;
id[i][j] = cnt+1, map[cnt+1][cnt+2] = a[i][j]-'0', cnt += 2;
}
}
s = 0, t = ++cnt;
for (int i = 1; i <= n; i++) {
scanf("%s", b[i]+1);
for (int j = 1; j <= m; j++) {
if (b[i][j] == 'L')	tot++, map[s][id[i][j]] = 1;
if (i-r < 1 || i+r > n || j-r < 1 || j+r > m)	map[id[i][j]+1][t] = INF;
if (a[i][j] != '0')	build(i, j);
}
}
printf("%d", tot-Dinic());
return 0;
}

posted @ 2017-09-20 15:36  Azrael_Death  阅读(195)  评论(0编辑  收藏  举报