【BZOJ1189】[HNOI2007]紧急疏散evacuate【最大流】【二分】

【题目链接】

先处理出每个人到每个门的最短距离。

二分答案mid。

从S到每个人连边,容量为1。

每个门拆为mid个点,第i个点代表第i个时刻的门,每个人向 第(最短距离)个点 ~ 第mid个点 连边。

每个门向T连边。

跑最大流,看是否满流。


一直担心建图错,结果没错,倒是忘了判impossible了。

/* Pigonometry */
#include 
#include 
#include 

using namespace std;

const int maxn = 50005, maxm = 400005, maxg = 22, maxq = 1000000, inf = 0x3f3f3f3f;

int n, m, k, tot, peo, mp[maxg][maxg], head[maxn], cur[maxn], cnt, q[maxq];
int bg, ed, dis[maxg][maxg][maxg << 2], id[maxg][maxg], door[maxg << 2][402], depth[maxn];
int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

struct _edge {
	int v, w, next;
} g[maxm << 1];

struct _data {
	int x, y, s;
} q2[maxq];

inline void getdis(int x, int y, int no) {
	int h = 0, t = 0;
	dis[x][y][no] = 0; q2[t++] = (_data){x, y, 0};
	while(h != t) {
		_data u = q2[h++];
		for(int i = 0; i < 4; i++) {
			int xx = u.x + dx[i], yy = u.y + dy[i];
			if(mp[xx][yy] != 1) continue;
			if(dis[xx][yy][no] > u.s + 1) {
				dis[xx][yy][no] = u.s + 1;
				q2[t++] = (_data){xx, yy, u.s + 1};
			}
		}
	}
}

inline void add(int u, int v, int w) {
	g[cnt] = (_edge){v, w, head[u]};
	head[u] = cnt++;
}

inline void insert(int u, int v, int w) {
	add(u, v, w); add(v, u, 0);
}

inline bool bfs() {
	for(int i = 0; i <= tot; i++) depth[i] = -1;
	int h = 0, t = 0, u, i; depth[q[t++] = bg] = 1;
	while(h != t) for(i = head[u = q[h++]]; ~i; i = g[i].next) if(g[i].w && !~depth[g[i].v]) {
		depth[g[i].v] = depth[u] + 1;
		if(g[i].v == ed) return 1;
		q[t++] = g[i].v;
	}
	return 0;
}

inline int dfs(int x, int flow) {
	if(x == ed) return flow;
	int left = flow;
	for(int i = cur[x]; ~i; i = g[i].next) if(g[i].w && depth[g[i].v] == depth[x] + 1) {
		int tmp = dfs(g[i].v, min(left, g[i].w));
		left -= tmp; g[i].w -= tmp; g[i ^ 1].w += tmp;
		if(g[i].w) cur[x] = i;
		if(!left) return flow;
	}
	if(left == flow) depth[x] = -1;
	return flow - left;
}

inline bool check(int mid) {
	for(int i = 0; i <= tot; i++) head[i] = -1; cnt = 0;

	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(mp[i][j] == 1)
		insert(bg, id[i][j], 1);
	for(int i = 1; i <= k; i++) for(int j = 1; j <= mid; j++)
		insert(door[i][j], ed, 1);
	for(int t = 1; t <= k; t++) for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++)
		if(mp[i][j] == 1 && dis[i][j][t] <= mid)
			for(int l = dis[i][j][t]; l <= mid; l++) insert(id[i][j], door[t][l], 1);

	int ans = peo;
	while(bfs()) {
		for(int i = 0; i <= tot; i++) cur[i] = head[i];
		ans -= dfs(bg, inf);
	}
	return !ans;
}

char str[maxg];

int main() {
	scanf("%d%d", &n, &m); k = peo = tot = 0;
	for(int i = 1; i <= n; i++) {
		scanf("%s", str + 1);
		for(int j = 1; j <= m; j++)
			if(str[j] == '.') mp[i][j] = 1, peo++;
			else if(str[j] == 'D') mp[i][j] = 2;
	}

	memset(dis, 0x3f, sizeof(dis));
	bg = ++tot; ed = ++tot;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++)
		if(mp[i][j] == 1) id[i][j] = ++tot;
		else if(mp[i][j] == 2) {
			getdis(i, j, ++k);
			for(int t = 1; t <= 400; t++) door[k][t] = ++tot;
		}

	int l = 0, r = 400, ans = inf;
	while(l <= r) {
		int mid = l + r >> 1;
		if(check(mid)) ans = mid, r = mid - 1;
		else l = mid + 1;
	}
	
	if(ans == inf) printf("impossible\n");
	else printf("%d\n", ans);
	return 0;
}

你可能感兴趣的:(二分,网络流)