poj 3057 Evacuation(二分+二分匹配,好题)

Evacuation
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2374 Accepted: 609

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

Source



题意:

一幅迷宫图'.'为空格,'D'为门,'#'为墙;
现在每个空格处有一人,且每个时刻每个门只能出去一人;当走到门时为离开房间;求所有人撤离的最短时间,否则impossible;


题解:

详细题解见挑战程序设计P230,感觉二分匹配好强大...



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int,int> P;
const int Maxn=18;
char ss[Maxn][Maxn];
int d[Maxn*Maxn][Maxn][Maxn],vis[Maxn][Maxn];
int g[Maxn*Maxn][Maxn*Maxn*Maxn];
bool used[Maxn*Maxn*Maxn];
int match[Maxn*Maxn*Maxn];
vector<pair<int,int> >man;
vector<pair<int,int> >door;
int n,m;
int nx[]={1,0,-1,0},ny[]={0,-1,0,1};
int vN,uN;

bool bfs(int u)
{
	for(int i=1;i<=vN;i++)
	{
		if(g[u][i]&&!used[i])
		{
			used[i]=true;
			if(match[i]<0||bfs(match[i]))
			{
				match[i]=u;
				return true;
			}
		}
	}
	return false;
}

int hungry()
{
	int ans=0;
	memset(match,-1,sizeof(match));
	for(int i=1;i<=uN;i++)
	{
		memset(used,false,sizeof(used));
		if(bfs(i)) ans++;
	}
	return ans;
}

void init()
{
	man.clear();
	door.clear();
	memset(d,0,sizeof(d));
}

int rbfs(int s)
{
	memset(vis,0,sizeof(vis));
	int x=door[s].first,y=door[s].second;
	d[s][x][y]=0;
	vis[x][y]=1;
	queue<pair<int,int> > q;
	q.push(make_pair(x,y));
	while(!q.empty())
	{
		P cur=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			int xx=cur.first+nx[i],yy=cur.second+ny[i];
			if(xx<=0||xx>n||yy<=0||yy>m) continue;
			if(vis[xx][yy]) continue;
			if(ss[xx][yy]=='X'||ss[xx][yy]=='D') continue;			
			vis[xx][yy]=1;
			d[s][xx][yy]=d[s][cur.first][cur.second]+1;
			q.push(make_pair(xx,yy));
		}
	}
}

bool ok(int t)
{
	memset(g,0,sizeof(g));
	for(int i=0;i<door.size();i++)
	{
		for(int j=0;j<man.size();j++)
		{
			int tt=d[i][man[j].first][man[j].second];
			if(tt<=t&&tt>0)
			for(int k=tt;k<=t;k++)
			g[j+1][door.size()*(k-1)+i+1]=1;
		}
	}
	uN=man.size(),vN=t*door.size()+uN;
	int ans=hungry();
	if(ans==man.size()) return true;
	else return false;
}

int main()
{
	int cas;
	scanf("%d",&cas);
	while(cas--)
	{
		init();
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++) 
		{
			scanf("%s",ss[i]+1);
			for(int j=1;j<=m;j++)
			{
				if(ss[i][j]=='D') door.push_back(make_pair(i,j));
				else if(ss[i][j]=='.') man.push_back(make_pair(i,j));
			}
		}
		if(door.size()==0&&man.size())	
		{
			puts("impossible");
			continue;
		}
		for(int i=0;i<door.size();i++) rbfs(i);
		bool flag=true;
		for(int i=0;i<man.size();i++)
		{
			bool have=false;
			for(int j=0;j<door.size();j++)
			{
				if(d[j][man[i].first][man[i].second]) have=true;
			}
			if(!have)
			{
				flag=false;
				break;
			}
		}
		if(!flag)
		{
			puts("impossible");
			continue;
		}	
		int l=-1,r=n*m+1;
		int ans;
		while(l+1<r)
		{
			int mid=(l+r)/2;
			if(ok(mid)) r=mid;
			else l=mid;
		}
		printf("%d\n",r);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值