Poj3084 panic room minimum cut

Source: Internet
Author: User
I feel that graph theory requires powerful YY capabilities... It is difficult to create a graph because this question is followed by a minimal cut process. 1: The room we want to protect in the question is the sink point T, and then we need to add an additional source point S; 2: There is an intruder room, we will connect it with the source point an arc (S, j, INF ). 3: For other rooms, if I-> J, we need to connect two arcs (I, j, INF) (J, I, 1). (here we need YY, because the source point is directly connected to the intruder room and the network is a directed graph, the problem is changed to removing the least weight and edge to make the source point and the sink point not connected, this is a minimal cut problem. Then the question is why we need to connect two arcs. Because the arc is directed, if the intruder is to be from I to J, because the lock is in room I, it cannot be closed anyway, so it is assigned as INF. However, if the intruder needs to enter from J to I, because the lock is in I, we only need to close a lock, so the arc value is 1. (See Figure YY below .... The intruder room is connected to the source point. If the arc from I to J is infinitely large, it is locked in I and T is the room we want to protect. As a result, the intruder is locked outside the door. If the arc I to J is 1, it means that closing a door can block this road .) If the maximum stream is greater than or equal to INF, no scheme exists. Otherwise, the maximum stream value is output. Panic Room
Time limit:1000 ms   Memory limit:65536 K
Total submissions:1585   Accepted:785

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 cocould 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 wocould 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. for example, if Room 3 had no intruder, and doors to Room 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 wocould 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
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;#define INF 0xFFFFFF#define MAXN 30struct edge{int to,c,next;};edge e[999999];char type[10];int head[MAXN],head2[MAXN],en;int n,m,st,ed,dis[MAXN],pre[MAXN],que[MAXN],maxflow;void add(int a,int b,int c){e[en].to=b;e[en].c=c;e[en].next=head[a];head[a]=en++;e[en].to=a;e[en].c=0;e[en].next=head[b];head[b]=en++;} bool bfs() {memset(dis,-1,sizeof(dis));que[0]=st,dis[st]=1;int t=1,f=0;while(f<t){int j=que[f++];for(int k=head[j];k!=-1;k=e[k].next){int i=e[k].to;if(dis[i]==-1 && e[k].c){que[t++]=i;dis[i]=dis[j]+1;if(i==ed) return true;}}}return false; } int update()  {  int p,flow=INF;      for (int i=pre[ed];i!=-1;i=pre[i])if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;        for (int i=pre[ed];i!=-1;i=pre[i]) e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;       maxflow+=flow;     return p;}  void dfs()  {  memset(pre,-1,sizeof(pre));memcpy(head2,head,sizeof(head2));      for(int i=st,j;i!=-1;)      {          int flag=false;          for(int k=head[i];k!=-1;k=e[k].next)              if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )            {                  pre[j]=i; head2[i]=k;  i=j; flag=true;                  if(i==ed) i=update();                  if(flag) break;            }          if (!flag) dis[i]=-1,i=pre[i];       }  } void solve(){int nn;scanf("%d%d",&n,&ed);st=n,en=0;memset(head,-1,sizeof(head));for(int i=0;i<n;i++){scanf("%s",type);if(type[0]=='I')add(st,i,INF);scanf("%d",&nn);for(int k=0;k<nn;k++){int j;scanf("%d",&j);add(i,j,INF),add(j,i,1);}}maxflow=0;while(bfs())dfs();if(maxflow<INF)printf("%d\n",maxflow);elseprintf("PANIC ROOM BREACH\n");}int main(){int t;scanf("%d",&t);while(t--)solve();return 0;} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.