20的数据量,这题是什么意思, 吓的我半死,一直还以为那里想错了.
其实这题就是个简单的最小割。
1 建立源点和汇点s,t
2 从s连一条权为INF的边到n(the panic room).
3 对于从 i 到 j 能开的门, 连一条权为INF 的(i->j)边,然后连一条权为1的(j->i) 的边。
然后求最小流就可以了.
Introduction
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 �C 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 �C 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
#include <stdio.h> #include <string> #include <string.h> #include <iostream> using namespace std; #define N 30 #define M N*N*N #define INF 0x3ffffff struct node { int to,w,next; }edge[M]; int cnt,pre[N]; int s,t; int n; int lv[N],gap[N]; int nn; void add_edge(int u,int v,int w) { edge[cnt].to=v; edge[cnt].w=w; edge[cnt].next=pre[u]; pre[u]=cnt++; } int sdfs(int k,int w) { if(k==t) return w; int f=0; int mi=nn-1; for(int p=pre[k];p!=-1;p=edge[p].next) { int v=edge[p].to; if(edge[p].w!=0) { if(lv[k]==lv[v]+1) { int tmp=sdfs(v,min(w-f,edge[p].w)); f+=tmp; edge[p].w-=tmp; edge[p^1].w+=tmp; if(f==w||lv[s]==nn) return f; } if(mi>lv[v]) mi=lv[v]; } } if(f==0) { gap[lv[k]]--; if(gap[lv[k]]==0) { lv[s]=nn; return f; } lv[k]=mi+1; gap[lv[k]]++; } return f; } int sap() { nn=t+1; int sum=0; memset(lv,0,sizeof(lv)); memset(gap,0,sizeof(gap)); gap[0]=nn; while(lv[s]<nn) { sum+=sdfs(s,INF); } return sum; } int main() { int t1,key; scanf("%d",&t1); while(t1--) { cnt=0; memset(pre,-1,sizeof(pre)); scanf("%d%d",&n,&key); s=0; t=n+1; key++; add_edge(key,t,INF); add_edge(t,key,0); for(int i=1;i<=n;i++) { char tmp[10]; scanf("%s",tmp); if(tmp[0]!='N') { add_edge(s,i,INF); add_edge(i,s,0); } int m; scanf("%d",&m); for(int j=0;j<m;j++) { int x; scanf("%d",&x); x++; add_edge(i,x,INF); add_edge(x,i,0); add_edge(x,i,1); add_edge(i,x,0); } } int ans=sap(); if(ans==INF) printf("PANIC ROOM BREACH\n"); else printf("%d\n",ans); } return 0; }