Panic Room
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1813 | Accepted: 928 |
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个房间和若干个门,给出每个房间能到达哪几个房间,即它们之间门的控制面板在该房间,该房间能随意到达门通向的房间。开始时所有门都是没有锁的,现在给出哪几个房间有入侵者,哪个房间需要受保护,要使得所有入侵者都不能进入受保护的那个房间,则至少要使多少个门上锁呢。
思路:求最小割。构图如下:需要受保护的房间设为汇点,虚拟一个超级源,源点连边到每个有入侵者的房间,容量为INF。对于有一门之隔的两个房间a和b,假设门的控制面板在a里面,则连两条边,一条是a->b,容量为INF,表示从a肯定能到达b,不能割这条边;另一条是b->a,容量为1,可以割这条边。最后求最大流即可。
AC代码:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <algorithm> #define ll __int64 using namespace std; const int INF = 1000000000; const int maxn = 1000; struct Edge{ int u, v, cap, flow, next; }et[maxn * maxn]; int low[maxn], pre[maxn], dis[maxn], cnt[maxn], cur[maxn], eh[maxn]; int s, t, num, n; void init(){ memset(eh, -1, sizeof(eh)); num = 0; } void add(int u, int v, int cap, int flow){ Edge e = {u, v, cap, flow, eh[u]}; et[num] = e; eh[u] = num++; } void addedge(int u, int v, int cap){ add(u, v, cap, 0); add(v, u, 0, 0); } int isap(int s, int t, int nv){ int u, v, now, flow = 0; memset(dis, 0, sizeof(dis)); memset(low, 0, sizeof(low)); memset(cnt, 0, sizeof(cnt)); for(u = 0; u <= nv; u++) cur[u] = eh[u]; low[s] = INF, cnt[0] = nv, u = s; while(dis[s] < nv) { for(now = cur[u]; now != -1; now = et[now].next) if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break; if(now != -1) { cur[u] = pre[v] = now; low[v] = min(low[u], et[now].cap - et[now].flow); u = v; if(u == t) { for(; u != s; u = et[pre[u]].u) { et[pre[u]].flow += low[t]; et[pre[u]^1].flow -= low[t]; } flow += low[t]; low[s] = INF; } } else { if(--cnt[dis[u]] == 0) break; dis[u] = nv, cur[u] = eh[u]; for(now = eh[u]; now != -1; now = et[now].next) if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1) dis[u] = dis[et[now].v] + 1; cnt[dis[u]]++; if(u != s) u = et[pre[u]].u; } } return flow; } int main() { int tt, k, a; char str[5]; scanf("%d", &tt); while(tt--) { scanf("%d%d", &n, &t); init(); s = n; for(int i = 0; i < n; i++) { scanf("%s", str); if(str[0] == 'I') addedge(s, i, INF); scanf("%d", &k); while(k--) { scanf("%d", &a); addedge(i, a, INF); addedge(a, i, 1); } } int ans = isap(s, t, n + 1); if(ans >= INF) printf("PANIC ROOM BREACH\n"); else printf("%d\n", ans); } return 0; }