poj 3057

二分图匹配,将各个时刻的门与人相连,跑个二分图就行了

Evacuation
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4753 Accepted: 1238

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

 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

const int maxn = 13;

int n,m,T,g[50000],dis[13][13][13][13];
int fx[]={-1,1,0,0},fy[]={0,0,-1,1};

char mp[maxn][maxn];

vector<int> px,py,dx,dy,G[50000];

bool vis[50000];

void adde(int u,int v){
    G[u].push_back(v);
}

bool dfs(int u){
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(!vis[v]){
            vis[v]=1;
            if(g[v]==-1||dfs(g[v])) {
                g[v]=u;
                return 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()){
        int sx=qx.front(),sy=qy.front();
        qx.pop();qy.pop();
        for(int i=0;i<4;i++){
            int ex=fx[i]+sx,ey=fy[i]+sy;
            if(ex>=0&&ex<n&&ey>=0&&ey<m&&mp[ex][ey]=='.'&&d[ex][ey]<0){
                d[ex][ey]=d[sx][sy]+1;
                qx.push(ex);qy.push(ey);
            }
        }
    }
}

void work(){
    px.clear();py.clear();
    dx.clear();dy.clear();
    memset(dis,-1,sizeof(dis));
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            if(mp[i][j]=='D'){
                dx.push_back(i);dy.push_back(j);
                bfs(i,j,dis[i][j]);
            }else if(mp[i][j]=='.'){
                px.push_back(i);py.push_back(j);
            }
        }
    int d=dx.size(),p=px.size();
    for(int i=0;i<=n*m*d+p;i++) G[i].clear();
    for(int i=0;i<d;i++)
        for(int j=0;j<p;j++){
            if(dis[dx[i]][dy[i]][px[j]][py[j]]>0) {
                for(int k=dis[dx[i]][dy[i]][px[j]][py[j]];k<=n*m;k++){
                    adde((k-1)*d+i,n*m*d+j);
                }
            }
        }
    if(p==0) {
        puts("0");
        return;
    }
    memset(g,-1,sizeof(g));
    int res=0;
    for(int i=0;i<n*m*d;i++){
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            if(++res==p) {
                printf("%d\n",i/d+1);
                return;
            }
    }
    puts("impossible");
    return ;
}

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++) scanf("%s",mp[i]);
        work();
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/plysc/p/11442353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值