POJ 3057 Evacuation(二分图匹配+BFS)
【题目链接】 http://poj.org/problem?id=3057
【题目大意】
给出一个迷宫,D表示门,.表示人,X表示不可通行,
每个门每时间单位只允许一个人通过,
每个人移动一格的为一时间单位的时间,
问所有人逃离这个迷宫的最小时间
【题解】
我们首先对于每个门进行搜索,求出其到每个人的最短时间,
之后我们将每扇门对于人来拆点,分别为这个人第几秒通过这个门
将拆点后的门对所有人做一遍顺序二分图匹配
匹配最终完成的时间的门是其第几个拆点那么时间就是第几秒
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int MAX_V=10000;
int V,match[MAX_V];
vector<int> G[MAX_V];
bool used[MAX_V];
void add_edge(int u,int v){
G[u].push_back(v);
G[v].push_back(u);
}
bool dfs(int v){
used[v]=1;
for(int i=0;i<G[v].size();i++){
int u=G[v][i],w=match[u];
if(w<0||!used[w]&&dfs(w)){
match[v]=u;
match[u]=v;
return 1;
}
}return 0;
}
const int dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
const int MAX_X=12,MAX_Y=12;
int X,Y;
char field[MAX_X][MAX_X+1];
vector<int> dX,dY;
vector<int> pX,pY;
int dist[MAX_X][MAX_Y][MAX_X][MAX_Y];
void bfs(int x,int y,int d[MAX_X][MAX_Y]){
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 x2=x+dx[k],y2=y+dy[k];
if(0<=x2&&x2<X&&0<=y2&&y2<Y&&field[x2][y2]=='.'&&d[x2][y2]<0){
d[x2][y2]=d[x][y]+1;
qx.push(x2);
qy.push(y2);
}
}
}
}
void solve(){
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(field[x][y]=='D'){
dX.push_back(x);
dY.push_back(y);
bfs(x,y,dist[x][y]);
}else if(field[x][y]=='.'){
pX.push_back(x);
pY.push_back(y);
}
}
}
int d=dX.size(),p=pX.size();
for(int i=0;i<=n*d+p;i++)G[i].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_edge((k-1)*d+i,n*d+j);
}
}
}
}
if(p==0){
puts("0");
return;
}
int num=0;
memset(match,-1,sizeof(match));
for(int v=0;v<n*d;v++){
memset(used,0,sizeof(used));
if(dfs(v)){
if(++num==p){
printf("%d\n",v/d+1);
return;
}
}
}puts("impossible");
}
int T;
void init(){
scanf("%d%d",&X,&Y);
for(int i=0;i<X;i++)scanf("%s",field[i]);
}
int main(){
scanf("%d",&T);
while(T--){
init();
solve();
}return 0;
}
POJ 3057 Evacuation(二分图匹配+BFS)的更多相关文章
- TTTTTTTTTTTTT poj 3057 Evacuation 二分图匹配+bfs
题意:见挑战230页 #include <iostream> #include <cstdio> #include <cstring> #include <c ...
- POJ 3057 Evacuation 二分图匹配
每个门每个时间只能出一个人,那就把每个门拆成多个,对应每个时间. 不断增加时间,然后增广,直到最大匹配. //#pragma comment(linker, "/STACK:10240000 ...
- POJ 3057 Evacuation (二分匹配)
题意:给定一个图,然后有几个门,每个人要出去,但是每个门每个秒只能出去一个,然后问你最少时间才能全部出去. 析:初一看,应该是像搜索,但是怎么保证每个人出去的时候都不冲突呢,毕竟每个门每次只能出一个人 ...
- POJ3057 Evacuation 二分图匹配+最短路
POJ3057 Evacuation 二分图匹配+最短路 题目描述 Fires can be disastrous, especially when a fire breaks out in a ro ...
- [poj] 3057 Evacuation
原题 题目大意 墙壁"X",空区域(都是人)".", 门"D". 人向门移动通过时视为逃脱,门每秒能出去一个人,人可以上下左右移动,墙阻止移 ...
- POJ 1274 裸二分图匹配
题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...
- POJ 2446 Chessboard (二分图匹配)
题意 在一个N*M的矩形里,用1*2的骨牌去覆盖该矩形,每个骨牌只能覆盖相邻的两个格子,问是否能把每个格子都盖住.PS:有K个孔不用覆盖. 思路 容易发现,棋盘上坐标和为奇数的点只会和坐标和为偶数的点 ...
- POJ 3057 Evacuation(二分匹配)
分析: 这是一个时间和门的二元组(t,d)和人p匹配的问题,当我们固定d0时,(t,d0)匹配的人数和t具有单调性. t增加看成是多增加了边就行了,所以bfs处理出p到每个d的最短时间,然后把(t,d ...
- POJ 3057 Evacuation 二分+最大流
Evacuation 题目连接: http://poj.org/problem?id=3057 Description Fires can be disastrous, especially when ...
随机推荐
- 第二阶段团队冲刺-two
昨天: 处理第一次启动服务器500的问题. 今天: 修复博客作业查询功能. 遇到的问题: 无.
- HDU 1937 J - Justice League
J - Justice League Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 1939 HE IS OFFSIDE
He is offside! Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 K: 大整数加法
http://acm.ocrosoft.com/problem.php?cid=1316&pid=10 题目描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200 ...
- aes加密码
js地址 https://github.com/yves8888/crypto-js 下面src<!DOCTYPE html> <html lang="en"&g ...
- SheetJS & Error: Sheet names cannot exceed 31 chars
SheetJS Error: Sheet names cannot exceed 31 chars title + version https://github.com/SheetJS/js-xlsx ...
- CSS3 em && rem 详细教程
1 # mobile css & rem & em & px > 1 rem === 16px 任意浏览器的默认字体高都是 16px, 所有未经调整的浏览器都符合: 1e ...
- IPv4地址分类及特征
IPv4地址分类及特征 IP地址后斜杠和数字代表的意思 其中有这样一个IP地址的格式:IP/数字,例如:111.222.111.222/24 这种格式平时在内网中用的不多,所以一下子看不懂,最后查了资 ...
- GCC 中 -L、-rpath和-rpath-link的区别
GCC 中 -L.-rpath和-rpath-link的区别 来源 http://blog.csdn.net/q1302182594/article/details/42102961 关于这3个参数的 ...
- 【POJ 2728 Desert King】
Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...