POJ 3084 - Panic Room【网络流 最小割】

来源:互联网 发布:网络导购员 编辑:程序博客网 时间:2024/05/19 01:10

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:

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:
  1. Start line – a single line "m n" (1 <=m<= 20; 0 <=n<= 19) where m indicates the number of rooms in the house and n indicates the room to secure (the panic room).
  2. 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

37 2NI 0I 3 0 4 5NI 2 1 6NI 2 1 2NI 0NI 0NI 07 2I 0NI 3 0 4 5NI 2 1 6I 2 1 2NI 0NI 0NI 04 3I 0NI 1 2NI 1 0NI 4 1 1 2 2

Sample Output

2PANIC ROOM BREACH1

Source

South Central USA 2006

1A~~感觉网络流做了26道题可算是入门了QAQ

题意:给出房间的单向上锁关系,为了防止所有人进入给定房间,需要至少加多少锁?(配合示例食用==

对于给定的u->v,因为这个方向是锁不住的,所以加oo边;但是反方向可以锁住,所以是流量=1;题中又说每个有人的屋子可能有不止一个人,那么增添超级源点,给每个有人的屋子加oo的边

    /*************    poj3084    2016.7.29    212K0MSC++2451B    *************/    #include <iostream>    #include<cstdio>    #include<cstring>    using namespace std;    const int mm=1000000;    const int mn=505*505*3;    const int oo=1000000000;    int node,src,dest,edge;    int reach[mm],flow[mm],nxt[mm];    int head[mn],work[mn],dis[mn],q[mn];    inline int min(int a,int b)    {        return a<b?a:b;    }    inline void prepare(int _node,int _src,int _dest)    {        node=_node,src=_src,dest=_dest;        for(int i=0;i<node;++i)head[i]=-1;        edge=0;    }    inline void addedge(int u,int v,int c1,int c2=0)    {        reach[edge]=v,flow[edge]=c1,nxt[edge]=head[u],head[u]=edge++;        reach[edge]=u,flow[edge]=c2,nxt[edge]=head[v],head[v]=edge++;    }    bool Dinic_bfs()    {        int i,u,v,l,r=0;        for(i=0;i<node;++i)dis[i]=-1;        dis[q[r++]=src]=0;        for(l=0;l<r;++l)            for(i=head[u=q[l]];i>=0;i=nxt[i])                if(flow[i]&&dis[v=reach[i]]<0)                {                    dis[q[r++]=v]=dis[u]+1;                    if(v==dest)return 1;                }        return 0;    }    int Dinic_dfs(int u,int exp)    {        if(u==dest)return exp;        for(int &i=work[u],v,tmp;i>=0;i=nxt[i])            if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)            {                flow[i]-=tmp;                flow[i^1]+=tmp;                return tmp;            }dis[u]--;        return 0;    }    int Dinic_flow()    {        int i,ret=0,delta;        while(Dinic_bfs())        {            for(i=0;i<node;++i)work[i]=head[i];            while(delta=Dinic_dfs(src,oo))ret+=delta;        }        return ret;    }int num[30][30];int main(){   // freopen("cin.txt","r",stdin);    int n,m,t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        prepare(n+1,n,m);        memset(num,0,sizeof(num));        for(int i=0;i<n;i++)        {            char str[3];            int c;            scanf("%s%d",str,&c);            if(str[0]=='I')addedge(n,i,oo);            while(c--)            {                int a;                scanf("%d",&a);                addedge(i,a,oo);                addedge(a,i,1);             //   printf("i=%d,a=%d\n",i,a);            }        }        int tmp=Dinic_flow();   //     printf("tmp=%d\n",tmp);        if(tmp>=oo)puts("PANIC ROOM BREACH");        else printf("%d\n",tmp);    }    return 0;}


0 0