Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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.
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 <=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).
- 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
题意:n个房间,房间之间有一些门,现在有入侵者进入某些房间,有一个房间是需要被保护的,如果a--b之间有门,门开的时候a,b互通,关上门之后a可以到b,但是b不能到a,一个简单的最小割
超级源点连接所有的入侵者进入的房间,流量为INF,不可切断,超级汇点就是要被保护的房间,中间有门的房间要稍微处理,因为关上门之后a还是可以到达b,所以如果ab之间有门,ab不能被切断,但是ba可以被切断
#include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; #define MAXN 200 #define MAXM 40000 #define INF 0x3f3f3f3f int n,m,head[MAXN],vis[MAXN],dis[MAXN],cur[MAXN],cnt,source,sink; struct node { int u,v,cap,flow,next; }edge[MAXM]; void init() { memset(head,-1,sizeof(head)); cnt=0; } void add(int u,int v,int w) { node E={u,v,w,0,head[u]}; edge[cnt]=E; head[u]=cnt++; node E1={v,u,0,0,head[v]}; edge[cnt]=E1; head[v]=cnt++; } void getMap() { source=0,sink=m+1; char op[5]; for(int i=1;i<=n;i++) { int num; scanf("%s%d",op,&num); while(num--) { int a; scanf("%d",&a); a++; add(i,a,INF); add(a,i,1); } if(op[0]=='I') add(source,i,INF); } } bool BFS(int s,int t) { queue<int>q; memset(vis,0,sizeof(vis)); memset(dis,-1,sizeof(dis)); dis[s]=0; vis[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=edge[i].next) { node E=edge[i]; if(!vis[E.v]&&E.cap>E.flow) { dis[E.v]=dis[E.u]+1; vis[E.v]=1; if(E.v==t) return true; q.push(E.v); } } } return false; } int DFS(int x,int a,int t) { int flow=0,f; if(a==0||t==x) return a; for(int &i=cur[x];i!=-1;i=edge[i].next) { node &E=edge[i]; if(dis[E.v]==dis[E.u]+1&&(f=DFS(E.v,min(E.cap-E.flow,a),t))>0) { a-=f; flow+=f; edge[i].flow+=f; edge[i^1].flow-=f; if(a==0) break; } } return flow; } int MAXflow(int s,int t) { int flow=0; while(BFS(s,t)) { memcpy(cur,head,sizeof(head)); flow+=DFS(s,INF,t); } return flow; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(); getMap(); int ans=MAXflow(source,sink); if(ans>=INF) printf("PANIC ROOM BREACH\n"); else printf("%d\n",ans); } return 0; }