zoukankan      html  css  js  c++  java
  • zoj 2788(最小割)

    20的数据量,这题是什么意思, 吓的我半死,一直还以为那里想错了. 

    其实这题就是个简单的最小割。

    1 建立源点和汇点s,t

    2 从s连一条权为INF的边到n(the panic room).

    3 对于从 i 到 j 能开的门, 连一条权为INF 的(i->j)边,然后连一条权为1的(j->i) 的边。

    然后求最小流就可以了.

    Panic Room

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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;
    }
  • 相关阅读:
    总结PHP缓存技术的多种方法
    超赞的Linux软件分享(持续更新)
    Android与IOS的优缺点比较 对 Android 与 IOS 比较是个个人的问题。 就好比我来说,我两个都用。我深知这两个平台的优缺点。所以,我决定分享我关于这两个移动平台的观点。另外,然后谈谈我对新的 Ubuntu 移动平台的印象和它的优势。 IOS 的优点 虽然这些天我是个十足的 Android 用户,但我必须承认 IOS 在某些方面做的是不错。首先,苹果公司在他们的设备更新方面有更
    简单说说JavaBean的使用
    mysql 压缩版安装
    分布式网站部署
    shiro启用注解方式
    ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务解决
    windows 下设置nginx负载均衡
    windows mysql 主从热备
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/2912880.html
Copyright © 2011-2022 走看看