博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3057 Evacuation 二分+最大流
阅读量:6671 次
发布时间:2019-06-25

本文共 3805 字,大约阅读时间需要 12 分钟。

Evacuation

题目连接:

Description

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.

Input

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

Output

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.

Sample Input

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

Sample Output

3

21
impossible

Hint

题意

一副迷宫图'.'为空格,'D'为门,'#'为墙;

现在每个空格处有一人,且每个时刻每个空格只能站一人;当走到门时为离开房间;求所有人撤离的最短时间,否则impossible;

题解:

二分答案,然后建图跑网络流去check就好了

二分t

S连向每个人,流量为1。每个人向t秒能到达的门流量为1。每个门向T连流量为t的边。然后看是不是满流。

代码

#include
#include
#include
#include
using namespace std;const int MAXN=100000,MAXM=100000,inf=1e9;struct Edge{ int v,c,f,nx; Edge() {} Edge(int v,int c,int f,int nx):v(v),c(c),f(f),nx(nx) {}} E[MAXM];int G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],N,sz;void init(int _n){ N=_n,sz=0; memset(G,-1,sizeof(G[0])*N);}void link(int u,int v,int c){ E[sz]=Edge(v,c,0,G[u]); G[u]=sz++; E[sz]=Edge(u,0,0,G[v]); G[v]=sz++;}bool bfs(int S,int T){ static int Q[MAXN]; memset(dis,-1,sizeof(dis[0])*N); dis[S]=0; Q[0]=S; for (int h=0,t=1,u,v,it;h
E[it].f) { dis[v]=dis[u]+1; Q[t++]=v; } } } return dis[T]!=-1;}int dfs(int u,int T,int low){ if (u==T) return low; int ret=0,tmp,v; for (int &it=cur[u];~it&&ret
E[it].f) { if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f))) { ret+=tmp; E[it].f+=tmp; E[it^1].f-=tmp; } } } if (!ret) dis[u]=-1; return ret;}int dinic(int S,int T){ int maxflow=0,tmp; while (bfs(S,T)) { memcpy(cur,G,sizeof(G[0])*N); while (tmp=dfs(S,T,inf)) maxflow+=tmp; } return maxflow;}char s[23][23];int dx[4]={1,-1,0,0};int dy[4]={0,0,1,-1};int n,m;int vis[23][23];int d[405][23][23];vector
doorx,doory;void doit(){ for(int i=0;i
qx,qy; d[i][x][y]=0; qx.push(x),qy.push(y); while(!qx.empty()) { x=qx.front(),y=qy.front(); qx.pop(),qy.pop(); for(int j=0;j<4;j++) { int nx=x+dx[j],ny=y+dy[j]; if(nx<0||nx>=n)continue; if(ny<0||ny>=m)continue; if(s[nx][ny]=='D'||s[nx][ny]=='X')continue; if(d[i][nx][ny]>d[i][x][y]+1) { d[i][nx][ny]=d[i][x][y]+1; qx.push(nx); qy.push(ny); } } } }}bool check(int t){ init(10005); int S = 9999,T = 10000; int cnt = 0; for(int i=0;i

转载地址:http://zloxo.baihongyu.com/

你可能感兴趣的文章
vim中的复制与粘贴 | WangYan BLog
查看>>
android.database.sqlite.SQLiteException: table TB_READ_PERIOD already exists
查看>>
Nginx 1.2.5 稳定版发布
查看>>
linux 自学系列:linux 文本模式
查看>>
poj1003
查看>>
Spring 表单处理
查看>>
编写用逻辑扇区号读写软盘的中断例程
查看>>
Pentaho Big Data Community Home - Pentaho Big Data - Pentaho Wiki
查看>>
HTML基础(二)
查看>>
【转】NSMutableArray的正确使用
查看>>
vim配置
查看>>
逆序数
查看>>
mysql远程访问的时候遇到了各种问题
查看>>
jQuery源码-美元背后的一点小技巧
查看>>
关于 多进程epoll 与 “惊群”问题
查看>>
Codeforces Round #175 (Div. 2) C. Building Permutation(贪心)
查看>>
使用任务计划程序自动执行任务
查看>>
IDEA在代码上无错误提示,但是编译时出现error:非法字符
查看>>
失业的程序员(八):创业的要素
查看>>
使用Beetle.Express简单构建高吞吐的TCP&UDP应用
查看>>