poj3084 Panic Room [最小割]

in 题目 with 0 comment

Description

You are the lead programmer for the Securitron 9042, the latest and greatest in home security software from Jellern Inc. (Motto: We secure your stuff so YOU can't even get to it). The software is designed to "secure" a room; it does this by determining the minimum number of locks it has to perform to prevent access to a given room from one or more other rooms. Each door connects two rooms and has a single control panel that will unlock it. This control panel is accessible from only one side of the door. So, for example, if the layout of a house looked like this:
3084_1.png
with rooms numbered 0-6 and control panels marked with the letters "CP" (each next to the door it can unlock and in the room that it is accessible from), then one could say that the minimum number of locks to perform to secure room 2 from room 1 is two; one has to lock the door between room 2 and room 1 and the door between room 3 and room 1. Note that it is impossible to secure room 2 from room 3, since one would always be able to use the control panel in room 3 that unlocks the door between room 3 and room 2.


Input

Input to this problem will begin with a line containing a single integer x indicating the number of datasets. Each data set consists of two components:
Start line – a single line "m n" $(1 \le m\le 20; 0 \le n\le 19) where m indicates the number of rooms in the house and n indicates the room to secure (the panic room).
Room list – a series of m lines. Each line lists, for a single room, whether there is an intruder in that room ("I" for intruder, "NI" for no intruder), a count of doors c (0 <= c <= 20) that lead to other rooms and have a control panel in this room, and a list of rooms that those doors lead to. For example, if room 3 had no intruder, and doors to rooms 1 and 2, and each of those doors' control panels were accessible from room 3 (as is the case in the above layout), the line for room 3 would read "NI 2 1 2". The first line in the list represents room 0. The second line represents room 1, and so on until the last line, which represents room m - 1. On each line, the rooms are always listed in ascending order. It is possible for rooms to be connected by multiple doors and for there to be more than one intruder!


Output

For each dataset, output the fewest number of locks to perform to secure the panic room from all the intruders. If it is impossible to secure the panic room from all the intruders, output "PANIC ROOM BREACH". Assume that all doors start out unlocked and there will not be an intruder in the panic room.


Sample Input

3
7 2
NI 0
I 3 0 4 5
NI 2 1 6
NI 2 1 2
NI 0
NI 0
NI 0
7 2
I 0
NI 3 0 4 5
NI 2 1 6
I 2 1 2
NI 0
NI 0
NI 0
4 3
I 0
NI 1 2
NI 1 0
NI 4 1 1 2 2


Sample Output

2
PANIC ROOM BREACH
1


Source

South Central USA 2006


Solution

最小割问题,建图过程:

  1. 指定超级源s=0,超级汇t=n+1(输入的点的编号均向后移)
  2. 每个被侵入点与超级源相连,边权为$\infty$
  3. 对于每个房间,与其可到达的房间连边,边权为$\infty$,因为无论怎么锁门都可以通过控制面板打开
  4. 对于每个房间,与其可到达的房间连反向边,边权为1(如果有多个,则叠加),表示被锁一次

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 25
#define INF 0x3f3f3f3f
#define DEBUG
using namespace std;
int n,s,t,cnt;
int mp[maxn][maxn];
int dis[maxn];

inline int read(int cnt)
{
    if(cnt)
    {
        char ch;
        int read=0,sign=1;
        do
            ch=getchar();
        while((ch<'0'||ch>'9')&&ch!='-');
        if(ch=='-') sign=-1,ch=getchar();
        while(ch>='0'&&ch<='9')
        {
            read=read*10+ch-'0';
            ch=getchar();
        }
        return sign*read;
    }
    else
    {
        char ch;
        do
            ch=getchar();
        while(ch!='N'&&ch!='I');
        if(ch=='N') return 0;
        else return 1;
    }
}

void init()
{
    memset(mp,0,sizeof(mp));
    s=0,n=read(1),t=read(1)+1,cnt=1;
    for(int i=1;i<=n;++i)
    {
        if(read(0)) mp[s][i]=INF;
        int x=read(1);
        for(int j=1;j<=x;++j)
        {
            int v=read(1)+1;
            mp[i][v]=INF;
            mp[v][i]+=1;
        }
    }
}

bool bfs()
{
    memset(dis,-1,sizeof(dis));
    queue<int> q;
    q.push(0);
    dis[0]=0;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=1;i<=n;++i)
        {
            if(mp[u][i]&&dis[i]<0)
            {
                dis[i]=dis[u]+1;
                q.push(i);
            }
        }
    }
    if(dis[t]<0) return 0;
    return 1;
}

int dfs(int x,int flow)
{
    if(x==t) return flow; 
    int used=0,w;
    for(int i=0;i<=n;++i)
    {
        if(mp[x][i]&&dis[i]==dis[x]+1)
        {
            w=flow-used;
            w=dfs(i,min(w,mp[x][i]));
            mp[x][i]-=w;
            mp[i][x]+=w;
            used+=w;
            if(used==flow) return flow;
        }
    }
    if(!used) dis[x]=-1;
    return used;
}

void dinic()
{
    int ans=0;
    while(bfs()) 
    {
        ans+=dfs(s,INF);
        if(ans>=INF) break;
    }
    if(ans<INF) printf("%d\n",ans);
    else printf("PANIC ROOM BREACH\n");
} 

int main()
{
    int T=read(1);
    while(T--)
    {
        memset(mp,0,sizeof(mp));
        s=0,n=read(1),t=read(1)+1,cnt=1;
        for(int i=1;i<=n;++i)
        {
            if(read(0)) mp[s][i]=INF;
            int x=read(1);
            for(int j=1;j<=x;++j)
            {
                int v=read(1)+1;
                mp[i][v]=INF;
                mp[v][i]+=1;
            }
        }
        dinic();
    }
    return 0;
}

扫描二维码,在手机上阅读!
Responses