Panic Room
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 1415 |
|
Accepted: 676 |
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:
- 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
这个题重在建图的思路。。。。。只要能想出合适的建图方式就可以了。。。。。
如果u能到v则(u,v)连一条inf容量的边(v,u)为 1 的边。。。。自己建一个超级源点到每一个有入侵者房间建一条inf的边。。。然后就用最大流来做就行了。。。如果结果比不比inf小就是无解。。。。有一个小优化就是输入的时候看有入侵者的房间能不能直接到达受保护的房间。。能的话就不能保护住。。。。直接输出即可。。
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
#define inf 1<<30
#define cc(m,v) memset(m,v,sizeof(m))
struct node{
int u,v,f,next;
}edge[1000];
int head[30],p=0,lev[30],cur[30];
int que[1000];
void ainit(){
p=0,cc(head,-1);
}
bool bfs(int s,int t){
int i,u,v,qin=0,qout=0;
cc(lev,0),lev[s]=1,que[qin++]=s;
while(qout!=qin){
u=que[qout++];
for(i=head[u];i!=-1;i=edge[i].next)
if(edge[i].f>0 && lev[v=edge[i].v]==0){
lev[v]=lev[u]+1,que[qin++]=v;
if(v==t) return 1;
}
}
return lev[t];
}
int dinic(int s,int t){
int qin=0,i,k,u,f;
int flow=0;
while(bfs(s,t)){
memcpy(cur,head,sizeof(head));
u=s,qin=0;
while(1){
if(u==t){
for(k=0,f=inf;k<qin;k++)
if(edge[que[k]].f<f)
f=edge[que[i=k]].f;
for(k=0;k<qin;k++)
edge[que[k]].f-=f,edge[que[k]^1].f+=f;
flow+=f,u=edge[que[qin=i]].u;
}
for(i=cur[u];cur[u]!=-1;i=cur[u]=edge[cur[u]].next)
if(edge[i].f>0 && lev[u]+1==lev[edge[i].v]) break;
if(cur[u]!=-1)
que[qin++]=cur[u],u=edge[cur[u]].v;
else{
if(qin==0) break;
lev[u]=-1,u=edge[que[--qin]].u;
}
}
}
return flow;
}
void addedge(int u,int v){
edge[p].u=u,edge[p].v=v,edge[p].f=inf,edge[p].next=head[u],head[u]=p++;
edge[p].u=v,edge[p].v=u,edge[p].f=1,edge[p].next=head[v],head[v]=p++;
}
int main(){
int cas,n,m,u,i,s,t;
bool flag=0;
char as[5];
scanf("%d",&cas);
while(cas--){
ainit();
scanf("%d%d",&n,&t);
for(flag=0,i=0,s=n;i<n;i++){
scanf("%s%d",as,&m);
if(as[0]=='I'){
addedge(s,i);
while(m--){
scanf("%d",&u);
if(u==t) flag=1;
addedge(i,u);
}
}else{
while(m--){
scanf("%d",&u);
addedge(i,u);
}
}
}
if(flag){
cout<<"PANIC ROOM BREACH"<<endl;continue;
}
int flo=dinic(s,t);
if(flo>=inf){
cout<<"PANIC ROOM BREACH"<<endl;continue;
}
printf("%d\n",flo);
}
return 0;
}