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,也就是每个单位时间只能向上下左右四个方向走一格. 多个人可以站在同一个格子上 ...
随机推荐
- portapack发射GPS的信号实现GPS脱机模拟器
要注意portapack必须要购买带高精度晶振的版本,另外固件要刷gridRF版本,用官方的或者havoc的都不行. 固件在这下载: 链接: https://pan.baidu.com/s/16flB ...
- BFART算法
参考:https://zhuanlan.zhihu.com/p/31498036 https://www.jianshu.com/p/83bb10ad1d32
- Java虚拟机性能调优(一)
Java虚拟机监控与调优,借助Java自带分析工具. jps:JVM Process Status Tool,显示指定系统内所有的HotSpot虚拟机进程 jstat:JVM Statistics M ...
- 120行代码打造.netcore生产力工具-小而美的后台异步组件
相信绝大部分开发者都接触过用户注册的流程,通常情况下大概的流程如下所示: 接收用户提交注册信息 持久化注册信息(数据库+redis) 发送注册成功短信(邮件) 写操作日志(可选) 伪代码如下: pub ...
- 「JOISC 2020 Day4」首都城市
题目 点这里看题目. 分析 做法比较容易看出来.我们对于每个城市,找出那些 " 如果这个城市在首都内,则必须在首都内的其它城市 " ,也就是为了让这个城市的小镇连通而必须选 ...
- 震惊!ConcurrentHashMap里面也有死循环,作者留下的“彩蛋”了解一下?
JDK BUG 这篇文章,聊一下我最近才知道的一个关于 JDK 8 的 BUG 吧. 首先说一下我是怎么发现这个 BUG 的呢? 大家都知道我对 Dubbo 有一定的关注,前段时间 Dubbo 2.7 ...
- 测试必备工具之最强抓包神器 Charles,你会了么?
前言 作为软件测试工程师,大家在工作中肯定经常会用到各种抓包工具来辅助测试,比如浏览器自带的抓包工具-F12,方便又快捷:比如时下特别流行的Fiddler工具,使用各种web和APP测试的各种场景 ...
- mingw32 exception在sjlj与dwarf差别-反汇编分析
sjlj (setjump/longjump)与dwarf-2为mingw32两种异常处理模型的实现.sjlj有着开销,而随linux发行的mingw32开发库包都是用sjlj版编译的,而Qt却采用d ...
- OS_进程调度:C++实现
实验二.进程调度模拟实验 一.实验目的: 本实验模拟在单处理机环境下的处理机调度,帮助理解进程调度的概念,深入了解进程控制块的功能,以及进程的创建.撤销和进程各个状态间的转换过程. 二.实验内容: 进 ...
- Beta 冲刺
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 Beta 冲刺 汇总博客 作业正文 如下 其他参考文献 ... 说明:此博客 ...