POJ 3084 Panic Room 最小割

有一些房间靠门连通,如果门被锁起来那么门就是单向的,否则是双向的,求最少锁起来的门数使某些房间和另外一个房间不连通。

最小割。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,j,k) for(i=j;i<=k;++i)
using namespace std;
const int inf = 0x3f3f3f3f, N = 21, M = 1605;

int level[N], cnt, cur[N], v[M], w[M], p[M], h[N], q[M], s, t;
void add(int a, int b, int c) {
    p[++cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt;
    p[++cnt] = h[b]; v[cnt] = a; w[cnt] = 0; h[b] = cnt;
}

bool bfs() {
    int f = 0, r = 0, u, i;
    memset(level, -1, sizeof level);
    q[r++] = s; level[s] = 1;
    while (f < r) {
        u = q[f++];
        for (i = h[u]; i; i = p[i]) {
            if (w[i] && level[v[i]] == -1) {
                level[v[i]] = level[u] + 1;
                q[r++] = v[i];
            }
        }
    }
    return level[t] != -1;
}

int dfs(int u, int low) {
    int i, tmp = 0, res = 0;
    if (u == t) return low;
    for (i = cur[u]; i && res < low; i = p[i]) {
        if (w[i] && level[v[i]] == level[u] + 1) {
            tmp = dfs(v[i], min(w[i], low - res));
            w[i] -= tmp; w[i ^ 1] += tmp; res += tmp;
            if (w[i]) cur[u] = i;
        }
    }
    if (!res) level[u] = -1;
    return res;
}

int dinic() {
    int ans = 0, i;
    while (bfs()) {
        for (i = 0; i <= s; ++i) cur[i] = h[i];
        ans += dfs(s, inf);
        if (ans == inf) return ans;
    }
    return ans;
}

int main() {
    int i, j, k, n, kase; char ch[8];
    scanf("%d", &kase);
    while (kase--) {
        scanf("%d%d", &n, &t);
        cnt = 1; s = n;
        memset(h, 0, sizeof h);
        for (i = 0; i < n; ++i) {
            scanf("%s%d", ch, &j);
            if (ch[0] == 'I') add(s, i, inf);
            while (j--) {
                scanf("%d", &k);
                add(i, k, inf);
                add(k, i, 1);
            }
        }
        int d = dinic();
        if (d == inf) puts("PANIC ROOM BREACH");
        else printf("%d\n", d);
    }
    return 0;
}

Panic Room

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2039 Accepted: 1054

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

Source

South Central USA 2006

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值