POJ3057 Evacuation 【二分匹配】

来源:互联网 发布:带虚化的拍照软件 编辑:程序博客网 时间:2024/05/22 07:49
EvacuationTime Limit: 1000MS      Memory Limit: 65536KTotal Submissions: 3050     Accepted: 766DescriptionFires 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.InputThe 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 roomOutputFor 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 Input35 5XXDXXX...XD...XX...DXXXXX5 12XXXXXXXXXXXXX..........DX.XXXXXXXXXXX..........XXXXXXXXXXXXX5 5XDXXXX.X.DXX.XXD.X.XXXXDXSample Output321impossibleSourceThe 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*12vector<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;}