POJ3057 Evacuation 【二分匹配】

Evacuation
Time Limit: 1000MS      Memory Limit: 65536K
Total Submissions: 3050     Accepted: 766
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

The 2006 Benelux Algorithm Programming Contest

显然如果 人i 距离 门j 的最短路为 dis
则 i 能在 >=dis 秒 到达 j
如果将 {xy} 作为一个个独立的元组
i能在dis秒达到j 则在 i 与 {disj}|(dis>=dis)
这就转化为一个2分匹配问题

二分法求能在>=x秒时疏散完人群
i能在dis秒达到j 则在 i 与 {disj}|(dis<=dis<=x)

而FF在求增广路时 假如搜索到左集合的 i 点 则对任意i>iii,都不会被使用
所以 直接将所有边添加到图中 若搜完i号点 匹配数=人数 则1+i/门数 就是答案

PS:HK求二分匹配只能老老实实二分x出来!!!!WA了一天啊!!!!

#include<stdio.h>
//#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<deque>
#include<queue>
#define ll long long
#define pii pair<int,int>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int inf=1e9+7;
const int N = 12*12*12*4+50;
//门数量<12*4,人数量<12*12
vector<int>G[2*N];
int match[2*N]; //match[u]和u是一个匹配
bool used[2*N];

inline void add_edge(int a,int b,int m){
    G[a].push_back(m+b);
    G[m+b].push_back(a);
}

bool dfs(int v){
    used[v]=true;
    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 true;
        }
    }
    return false;
}


char mp[15][15];
vector<pii>per,door;
int dist[15][15][15][15];
const int dirX[]={0,0,-1,1};
const int dirY[]={1,-1,0,0};

void bfs(int x,int y,int dis[15][15],int n,int m){
    deque<int>q1,q2;
    q1.push_back(x);
    q2.push_back(y);
    dis[x][y]=0;
    while(!q1.empty()){
        int ux=q1.front();
        int uy=q2.front();
        q1.pop_front();
        q2.pop_front();
        for(int i=0;i<4;++i){
            int vx=ux+dirX[i];
            int vy=uy+dirY[i];
            if(vx>=0&&vy>=0&&vx<n&&vy<m&&mp[vx][vy]=='.'&&dis[vx][vy]==0x3f3f3f3f){
                dis[vx][vy]=dis[ux][uy]+1;
                q1.push_back(vx);
                q2.push_back(vy);
            }
        }
    }
}

void buildG(int n,int m){
    MEM(dist,0x3f);
    for(int i=0;i<door.size();++i){
        int a=door[i].first,b=door[i].second;
        bfs(a,b,dist[a][b],n,m);
        for(int j=0;j<per.size();++j){
            int c=per[j].first,d=per[j].second;
            int st=dist[a][b][c][d];
            while(st<=per.size()){
                add_edge((st-1)*door.size()+i,j,per.size()*door.size());
                ++st;
            }
        }
    }
}

void slove(int n,int m){
    per.clear();
    door.clear();
    for(int i=0;i<2*N;++i){
        G[i].clear();
    }
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            if(mp[i][j]=='D'){
                door.push_back(make_pair(i,j));
            }
            if(mp[i][j]=='.'){
                per.push_back(make_pair(i,j));
            }
        }
    }
    if(per.size()==0){
        puts("0");
        return;
    }
    int ans=-1;
    buildG(n,m);
    int res=0;
    MEM(match,-1);
    for(int i=0;i<door.size()*per.size();++i){
        if(match[i]<0){
            MEM(used,0);
            res+=dfs(i);
            if(res==per.size()){
                ans=1+i/door.size();
                break;
            }
        }
    }
    if(ans==-1){
        puts("impossible");
    }
    else{
        printf("%d\n",ans);
    }
}

int main()
{
    //freopen("/home/lu/code/r.txt","r",stdin);
    int T,n,m;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i){
            scanf("%s",mp[i]);
        }
        slove(n,m);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值