题目

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 题解的更多相关文章

  1. POJ 3057 Evacuation 二分+最大流

    Evacuation 题目连接: http://poj.org/problem?id=3057 Description Fires can be disastrous, especially when ...

  2. POJ 3057 Evacuation(二分图匹配+BFS)

    [题目链接] http://poj.org/problem?id=3057 [题目大意] 给出一个迷宫,D表示门,.表示人,X表示不可通行, 每个门每时间单位只允许一个人通过, 每个人移动一格的为一时 ...

  3. 【最大匹配+二分答案】POJ 3057 Evacuation

    题目大意 POJ链接 有一个\(X×Y\)的房间,X代表墙壁,D是门,.代表人.这个房间着火了,人要跑出去,但是每一个时间点只有一个人可以从门出去. 问最后一个人逃出去的最短时间,如果不能逃出去,输出 ...

  4. POJ 3057 Evacuation 二分图匹配

    每个门每个时间只能出一个人,那就把每个门拆成多个,对应每个时间. 不断增加时间,然后增广,直到最大匹配. //#pragma comment(linker, "/STACK:10240000 ...

  5. POJ 3057 Evacuation (二分匹配)

    题意:给定一个图,然后有几个门,每个人要出去,但是每个门每个秒只能出去一个,然后问你最少时间才能全部出去. 析:初一看,应该是像搜索,但是怎么保证每个人出去的时候都不冲突呢,毕竟每个门每次只能出一个人 ...

  6. [poj] 3057 Evacuation

    原题 题目大意 墙壁"X",空区域(都是人)".", 门"D". 人向门移动通过时视为逃脱,门每秒能出去一个人,人可以上下左右移动,墙阻止移 ...

  7. POJ 3057 Evacuation(二分匹配)

    分析: 这是一个时间和门的二元组(t,d)和人p匹配的问题,当我们固定d0时,(t,d0)匹配的人数和t具有单调性. t增加看成是多增加了边就行了,所以bfs处理出p到每个d的最短时间,然后把(t,d ...

  8. TTTTTTTTTTTTT poj 3057 Evacuation 二分图匹配+bfs

    题意:见挑战230页 #include <iostream> #include <cstdio> #include <cstring> #include <c ...

  9. POJ 3057 网络流 Evacuation

    题意: 有一个n×m的房间,四周每个格子要么是墙要么是门.中间部分是墙或者人. 现在所有人要从房间逃出去,每个人的速度为1,也就是每个单位时间只能向上下左右四个方向走一格. 多个人可以站在同一个格子上 ...

随机推荐

  1. js-ajax方法详解以及封装

    本文主要从使用ajax请求的步骤.ajax状态码和http响应状态码以及ajax封装三个方面阐述 一.使用ajax请求的步骤 // 一.创建 XMLHttpRequest 对象 var xhr = n ...

  2. linux系统判断内存是否达到瓶颈的小技巧

    1.linux下最常用的系统状态监控工具top 工具,可以使用top -c 来进行查看当前内存的占用情况 free 为内存的剩余状态,当前为3.8G的空闲内存,总的物理内存是8G,按键 shift+m ...

  3. iOS-Code Data多线程的封装详解

    Code Data 的单例封装:      很容易发现,系统生成的模版代码将Core Data 的基本的准备(貌似还挺复杂!)都放在 AppDelegate中了,可苹果公司为什么会这么做呢?     ...

  4. 计算从1到n中,出现某位数字的次数

    出现1-9中某位数字次数的算法 /** * @param input 整数n(1 ≤ n ≤ 1,000,000,000) * @return 1-9中某个数字在数列中出现的次数 */ public ...

  5. win10系统下office 2019激活

    1.新建一个文本文件,创建批处理文件office.bat @echo off (cd /d "%~dp0")&&(NET FILE||(powershell sta ...

  6. pikachu 搭建

    一:首先下载XAMPP 1.先到官方网站安装XAMPP  https://www.apachefriends.org/zh_cn/index.html 选择适合自己的电脑系统下载,本次windows系 ...

  7. belongsTo和hasOne的区别

    简单的讲就是,没有太大的区别,只是在逻辑上出现的思想的偏差(逻辑的合理性).belongsTo:可以理解为属于hasOne:可以理解为拥有 首先,我们创建两张表.user表 字段 id name pa ...

  8. cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort

    cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort sort(b,e) sort(b,e,p) stable_sort(b,e) stable_sort(b,e,p) ...

  9. CVE-2018-12613 phpmyadmin文件包含getshell连载(三)

    这是phpmyadmin系列渗透思路的第三篇文章,前面一篇文章阐述了通过慢查询日志getshell,本文将通过文件包含漏洞展开讨论 #001 影响版本 Phpmyadmin 4.8.0/4.8.0.1 ...

  10. 新老单点的改造——-理解Cookie、Session、Token

    近期参与了新老单点的改造,一直想总结一下,发现这篇文章比较贴切. 整理了如下: 随着交互式Web应用的兴起,像在线购物网站,需要登录的网站等等,马上就面临一个问题,那就是要管理会话,必须记住哪些人登录 ...