• Evacuation (二分图)


    Evacuation (二分图)

    Describe

    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
    

    Summary

    有一个X*Y的房间,‘X’代表墙壁,‘D’是门,‘.’代表人。这个房间着火了,人要跑出去,但是每一个时间点只有一个人可以从门出去。
    问最后一个人逃出去的最短时间,如果不能逃出去,输出impossible。

    Solution

    以前有一道小杉的题,也是逃跑,但是那个题的门被使用一次就消失了,这个可以多次使用,不过每个时刻只能有一人使用,其实本质上差不多。

    首先我们要 Bfs 一遍,以每个门为起点,找人到门的最短距离,如果有人无法到达所有门,那么直接输出"impossible"。

    然后我们不断增加时间,每个时间我们增加门的数量,增加的数量就是门的总数,然后在这个时间内可以逃脱的人向门连一条边,然后我们进行二分图匹配。如果得出的结果跟人数一样,那么这个时间就是最小时间,否则,我们在重复之前的操作。

    直到最大匹配得出的结果和人数一样为止。

  • 相关阅读:
    BestCoder 2nd Anniversary/HDU 5718 高精度 模拟
    FZU 2168 前缀和+dp递推
    poj 1088 记忆化搜索
    Codeforces Round #241 (Div. 2) B dp
    poj 3053 优先队列处理
    取消修改
    csv乱码
    iconv()
    cakephp中sql查询between
    虚拟机上linux与windows之间复制粘贴
  • 原文地址:https://www.cnblogs.com/Lour688/p/12899602.html
Copyright © 2020-2023  润新知