POJ 3057 Evacuation 题解
题目
Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time, it may take a while for everyone to escape.
You are given the floorplan of a room and must find out how much time it will take for everyone to get out. Rooms consist of obstacles and walls, which are represented on the map by an 'X', empty squares, represented by a '.' and exit doors, which are represented by a 'D'. The boundary of the room consists only of doors and walls, and there are no doors inside the room. The interior of the room contains at least one empty square.
Initially, there is one person on every empty square in the room and these persons should move to a door to exit. They can move one square per second to the North, South, East or West. While evacuating, multiple persons can be on a single square. The doors are narrow, however, and only one person can leave through a door per second.
What is the minimal time necessary to evacuate everybody? A person is evacuated at the moment he or she enters a door square.
输入格式
The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:
One line with two integers \(Y\) and \(X\), separated by a single space, satisfying \(3 <= Y, X <= 12\): the size of the room
\(Y\) lines with \(X\) characters, each character being either 'X', '.', or 'D': a valid description of a room
输出格式
For every test case in the input, the output should contain a single line with the minimal evacuation time in seconds, if evacuation is possible, or "impossible", if it is not.
样例输入
3
5 5
XXDXX
X...X
D...X
X...D
XXXXX
5 12
XXXXXXXXXXXX
X..........D
X.XXXXXXXXXX
X..........X
XXXXXXXXXXXX
5 5
XDXXX
X.X.D
XX.XX
D.X.X
XXXDX
样例输出
3
21
impossible
题解
这道题要用二分图的最大匹配来做
对于每一扇门,同一秒只能让一个人通过,那我们就为每一扇门在每一秒建一个节点。
对于每一个人,假设他最快可以在 \(t\) 的时间到达门 \(i\),那就把\(t\)到最大时间(图的大小) 这些节点建边连到人 \(j\)。
如果在这个图上跑二分匹配,匹配的边 <\(t\)时的门\(i\), 人\(j\)> 就代表人 \(j\) 会在时间 \(t\) 时从门 \(i\) 逃脱。
找最短时间, 不断地枚举点,从该点找增广路。当从时间小的门的点开始枚举,这就代表时间小的门会优先匹配。我们只需看看枚举到哪个时间的门的时候,总匹配数等于人的数量,也是就所有人都被匹配到时,程序就可以结束了。答案就是此时的时间。
实现的时候, 可以从每个门做一次 bfs,来找各个人到各个点所需的时间
代码
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int V, X, Y, dxy[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char map[13][13];
vector<int> G[50010], dx, dy, px, py;
int match[50005], dist[13][13][13][13];
bool vis[50005];
void add(int u, int v) { G[u].push_back(v), G[v].push_back(u); }
bool find(int v) {
vis[v] = 1;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
if (match[u] == -1 || !vis[match[u]] && find(match[u]))
return match[v] = u, match[u] = v, true;
}
return false;
}
void bfs(int x, int y, int d[13][13]) {
queue<int> qx, qy;
d[x][y] = 0;
qx.push(x), qy.push(y);
while (!qx.empty()) {
x = qx.front(), qx.pop();
y = qy.front(), qy.pop();
for (int k = 0; k < 4; k++) {
int nx = x + dxy[k][0], ny = y + dxy[k][1];
if (0 <= nx && nx < X && 0 <= ny && ny < Y && map[nx][ny] == '.' && d[nx][ny] < 0) {
d[nx][ny] = d[x][y] + 1;
qx.push(nx), qy.push(ny);
}
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &X, &Y);
for (int i = 0; i < X; i++) scanf("%s", map[i]);
int n = X * Y;
dx.clear(), dy.clear(), px.clear(), py.clear();
memset(dist, -1, sizeof(dist));
for (int x = 0; x < X; x++)
for (int y = 0; y < Y; y++) {
if (map[x][y] == 'D') {
dx.push_back(x), dy.push_back(y), bfs(x, y, dist[x][y]);
} else if (map[x][y] == '.')
px.push_back(x), py.push_back(y);
}
int d = dx.size(), p = px.size();
V = X * Y * d + p;
for (int v = 0; v < V; ++v) G[v].clear();
for (int i = 0; i < d; i++)
for (int j = 0; j < p; j++)
if (dist[dx[i]][dy[i]][px[j]][py[j]] >= 0)
for (int k = dist[dx[i]][dy[i]][px[j]][py[j]]; k <= n; k++)
add((k - 1) * d + i, n * d + j);
if (p == 0) {
puts("0");
continue;
}
int res = 0, flag = 1;
memset(match, -1, sizeof(match));
for (int v = 0; v < n * d; v++) {
memset(vis, 0, sizeof(vis));
if (find(v) && ++res == p) {
printf("%d\n", v / d + 1);
flag = 0;
break;
}
}
if(flag)puts("impossible");
}
}
POJ 3057 Evacuation 题解的更多相关文章
- POJ 3057 Evacuation 二分+最大流
Evacuation 题目连接: http://poj.org/problem?id=3057 Description Fires can be disastrous, especially when ...
- POJ 3057 Evacuation(二分图匹配+BFS)
[题目链接] http://poj.org/problem?id=3057 [题目大意] 给出一个迷宫,D表示门,.表示人,X表示不可通行, 每个门每时间单位只允许一个人通过, 每个人移动一格的为一时 ...
- 【最大匹配+二分答案】POJ 3057 Evacuation
题目大意 POJ链接 有一个\(X×Y\)的房间,X代表墙壁,D是门,.代表人.这个房间着火了,人要跑出去,但是每一个时间点只有一个人可以从门出去. 问最后一个人逃出去的最短时间,如果不能逃出去,输出 ...
- POJ 3057 Evacuation 二分图匹配
每个门每个时间只能出一个人,那就把每个门拆成多个,对应每个时间. 不断增加时间,然后增广,直到最大匹配. //#pragma comment(linker, "/STACK:10240000 ...
- POJ 3057 Evacuation (二分匹配)
题意:给定一个图,然后有几个门,每个人要出去,但是每个门每个秒只能出去一个,然后问你最少时间才能全部出去. 析:初一看,应该是像搜索,但是怎么保证每个人出去的时候都不冲突呢,毕竟每个门每次只能出一个人 ...
- [poj] 3057 Evacuation
原题 题目大意 墙壁"X",空区域(都是人)".", 门"D". 人向门移动通过时视为逃脱,门每秒能出去一个人,人可以上下左右移动,墙阻止移 ...
- POJ 3057 Evacuation(二分匹配)
分析: 这是一个时间和门的二元组(t,d)和人p匹配的问题,当我们固定d0时,(t,d0)匹配的人数和t具有单调性. t增加看成是多增加了边就行了,所以bfs处理出p到每个d的最短时间,然后把(t,d ...
- TTTTTTTTTTTTT poj 3057 Evacuation 二分图匹配+bfs
题意:见挑战230页 #include <iostream> #include <cstdio> #include <cstring> #include <c ...
- POJ 3057 网络流 Evacuation
题意: 有一个n×m的房间,四周每个格子要么是墙要么是门.中间部分是墙或者人. 现在所有人要从房间逃出去,每个人的速度为1,也就是每个单位时间只能向上下左右四个方向走一格. 多个人可以站在同一个格子上 ...
随机推荐
- python自学Day05(自学书籍python编程从入门到实践)
第6章 字典 6.1 一个简单的字典 先跟随书本创建一个简单的字典感受一下. alien_0 = {'color':'green','points':5} print(alien_0['color'] ...
- 移除VS解决方案中的TFS版本控制
项目每次会弹出提示 正在打开的解决方案已绑定到以下 Azure DevOps Server 上的源代码管理: xxxxx.是否要联系此服务器以尝试启用源代码管理集成? 移除VS解决方案中的TFS版本控 ...
- 5.keras-Dropout剪枝操作的应用
keras-Dropout剪枝操作的应用 1.载入数据以及预处理 import numpy as np from keras.datasets import mnist from keras.util ...
- Java创建ES索引实现
1.pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- mac 排查被占端口
Last login: Wed Sep :: on ttys000 sam:~ sam$ sudo lsof -i : Password: COMMAND PID USER FD TYPE DEVIC ...
- rollup环境搭建(es6转es5、压缩、本地服务器、热更新)
文件目录 package.json { "name": "my-vue", "version": "1.0.0", &q ...
- IAT表
0X0 0 DLL介绍 DLL翻译器为动态链接库,原来不存在DLL的概念只有,库的概念,编译器会把从库中获取的二进制代码插入到应用程序中.在现在windows操作系统使用了数量庞大的库函数(进程,内存 ...
- Excel数据透视表的日常应用技巧
对工作表中数据进行统计是经常需要的.一般情况我们都是使用菜单命令或函数来进行数据的统计的.可是如果要统计的工作表中记录很多,而且需要统计的项目也很多时,使用这种方法就显得力不从心了.请问还有什么更好的 ...
- .NETCore微服务探寻(一) - 网关
前言 一直以来对于.NETCore微服务相关的技术栈都处于一个浅尝辄止的了解阶段,在现实工作中也对于微服务也一直没有使用的业务环境,所以一直也没有整合过一个完整的基于.NETCore技术栈的微服务项目 ...
- 利用Azure Functions和k8s构建Serverless计算平台
题记:昨晚在一个技术社区直播分享了"利用Azure Functions和k8s构建Serverless计算平台"这一话题.整个分享分为4个部分:Serverless概念的介绍.Az ...