bzoj 1189 [HNOI2007]紧急疏散evacuate

5 5
XXXXX
X…D
XX.XX
X..XX
XXDXX

3

题解

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 50010
using namespace std;
const int inf = 0x3f3f3f3f;
struct flows {
struct node {
int to, next, flow;
} e[N * 20];
int tot, st[N], dis[N], cur[N];
void init() {
tot = -1, memset(e, -1, sizeof e),
memset(st, -1, sizeof st);
}
void add(int x, int y, int z) {
e[++tot].to = y;
e[tot].flow = z;
e[tot].next = st[x];
st[x] = tot;
}
void add_edge(int x, int y, int z) {
//      printf("add:%d %d %d\n", x, y, z);
}
queue <int> que;
int bfs(int S, int T) {
memcpy(cur, st, sizeof st);
memset(dis, 0, sizeof dis);
while (!que.empty()) que.pop();
que.push(S);
dis[S] = 1;
while(!que.empty()) {
int now = que.front();
que.pop();
for (int i = st[now]; ~i; i = e[i].next)
if (e[i].flow && !dis[e[i].to]) {
dis[e[i].to] = dis[now] + 1;
if (e[i].to == T) return 1;
que.push(e[i].to);
}
}
return 0;
}
int finds(int now, int T, int flow) {
if (now == T)
return flow;
int f;
for (int i = cur[now]; ~i; i = e[i].next) {
cur[now] = i;
if (e[i].flow && dis[e[i].to] == dis[now] + 1 &&
(f = finds(e[i].to, T, min(flow, e[i].flow)))) {
e[i].flow -= f;
e[i^1].flow += f;
return f;
}
}
return 0;
}
int dinic(int S, int T) {
int flow = 0, x;
while(bfs(S, T)) {
while(x = finds(S, T, inf)) {
flow += x;
}
}
return flow;
}
}flow;
int n, m, S, T, x, y, z, c, cnt;
char s[30];
int map[30][30], dis[1000][21][21], vis[21][21];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct data {
int x, y, s;
data() {}
data(int a, int b, int c) : x(a), y(b), s(c) {}
};
queue <data> que;
bool judge(int x, int y) {
if (x >= 1 && x <= n && y >= 1 && y <= m && map[x][y] < 0 && !vis[x][y])
return 1;
return 0;
}
int ge(int x, int y) { return (x - 1) * m + y; }
bool check(int x) {
flow.init();
S = 0, T = n * m + 1;
int all = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (map[i][j] < 0)
for (int i = 1; i <= cnt; ++i) {
for (int j = 1; j <= n; ++j)
for (int k = 1; k <= m; ++k)
if (map[j][k] < 0 && dis[i][j][k] <= x)
flow.add_edge(ge(j, k), (i - 1) * x + n * m + dis[i][j][k], 1);
}
for (int i = 1; i <= cnt; ++i)
for (int j = 1; j <= x; ++j) {
int now = (i - 1) * x + n * m;
if (j != x)
flow.add_edge(now + j, now + j + 1, inf);
}
return flow.dinic(S, T) == all;
}
main() {
scanf("%d%d", &n, &m);
memset(dis, 0x3f, sizeof dis);
for (int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
for (int j = 1; j <= m; ++j)
if (s[j] == '.')
map[i][j] = -1;
else if (s[j] == 'D')
map[i][j] = ++cnt;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (map[i][j] > 0) {
int t = map[i][j];
//              dis[t][0][0] = i, dis[t][0][1] = j;
while (!que.empty()) que.pop();
que.push(data(i, j, 0)); dis[t][i][j] = 0;
memset(vis, 0, sizeof vis);
while (!que.empty()) {
data now = que.front(); que.pop();
for (int k = 0; k < 4; ++k)
if (judge(now.x + dir[k][0], now.y + dir[k][1]))
dis[t][now.x + dir[k][0]][now.y + dir[k][1]] = now.s + 1,
vis[now.x + dir[k][0]][now.y + dir[k][1]] = 1,
que.push(data(now.x + dir[k][0], now.y + dir[k][1], now.s + 1));
}
}
int l = 0, r = 1000, ans = -1;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid))
r = mid - 1, ans = mid;
else
l = mid + 1;
}
if (ans > 0)
printf("%d\n", ans);
else
puts("impossible");
}