zoukankan      html  css  js  c++  java
  • 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
    ",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
  • 相关阅读:
    [置顶] java得到前一个月的年月日时分秒
    Windows 8.1 Preview的新功能和新API
    oracle保证读一致性原理
    10161
    Qt国际化
    使用Maven管理依赖JAR文件,自定义项目布局,利用ANT生成不同的发布包
    Haxe2.10到Haxe3,NME到OpenFL的迁移备忘
    设置RichEdit相关颜色说明
    使用MFC CImage类绘制PNG图片时遇到的问题
    把网球计分招式重构到状态模式
  • 原文地址:https://www.cnblogs.com/plysc/p/11442353.html
Copyright © 2011-2022 走看看