返回顶部
大江东去,浪淘尽,千古风流人物。故垒西边,人道是,三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。江山如画,一时多少豪杰。遥想公瑾当年,小乔初嫁了,雄姿英发。羽扇纶巾,谈笑间,樯橹灰飞烟灭。故国神游,多情应笑我,早生华发。人生如梦,一尊还酹江月。

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

 

posted @ 2019-09-01 15:03  plysc  阅读(172)  评论(0编辑  收藏  举报