首页 > 代码库 > Hackers’ Crackdown - UVa11825 状压dp

Hackers’ Crackdown - UVa11825 状压dp

Problem H

Hackers’ Crackdown 
Input: 
Standard Input

Output: Standard Output

 

Miracle Corporations has a number of system services running in a distributed computer system which is a prime target for hackers. The system is basically a set of computer nodes with each of them running a set of services. Note that, the set of services running on every node is same everywhere in the network. A hacker can destroy a service by running a specialized exploit for that service in all the nodes.

 

One day, a smart hacker collects necessary exploits for all these services and launches an attack on the system. He finds a security hole that gives him just enough time to run a single exploit in each computer. These exploits have the characteristic that, its successfully infects the computer where it was originally run and all the neighbor computers of that node.

 

Given a network description, find the maximum number of services that the hacker can damage.

 

Input

There will be multiple test cases in the input file. A test case begins with an integer N (1<=N<=16), the number of nodes in the network. The nodes are denoted by 0 to N - 1. Each of the following lines describes the neighbors of a node. Line i (0<=i<N)represents the description of node i. The description for node starts with an integer m (Number of neighbors for node i), followed byintegers in the range of to N - 1, each denoting a neighboring node of node i.

 

The end of input will be denoted by a case with N = 0. This case should not be processed.

 

Output

For each test case, print a line in the format, “Case X: Y”, where X is the case number & Y is the maximum possible number of services that can be damaged.

                                                 

Sample Input

Output for Sample Input

3

2 1 2

2 0 2

2 0 1

4

1 1

1 0

1 3

1 2

0

Case 1: 3

Case 2: 2

 

题意:有n个电脑,一起执行1-n种工程,对于每台电脑,你有一次机会使其不能工作某一项工程,同时与它直接连接的电脑有收到该影响。如果一项工程没有电脑执行,那么这项工程就瘫痪了,问最多能瘫痪多少工程。

思路:首先每天电脑及其直接连接的先状压一下,表示使哪些瘫痪。S表示当前已经使哪些电脑瘫痪过的最大损坏工程数,如果S-S2可以瘫痪一个工程的话,那么dp[S]=max(dp[S],dp[S2]+1)。这里还有一个求子集的实现技巧见代码。

AC代码如下:

[cpp] view plaincopy
    1. #include<cstdio>  
    2. #include<cstring>  
    3. #include<algorithm>  
    4. using namespace std;  
    5. int n,m,pow2[20],val[20],c[100010],dp[100010];  
    6. int main()  
    7. {  
    8.     int t=0,i,j,k,total;  
    9.     pow2[0]=1;  
    10.     for(i=1;i<=16;i++)  
    11.        pow2[i]=pow2[i-1]*2;  
    12.     while(~scanf("%d",&n) && n>0)  
    13.     {  
    14.         for(i=0;i<n;i++)  
    15.         {  
    16.             val[i]=pow2[i];  
    17.             scanf("%d",&m);  
    18.             for(j=1;j<=m;j++)  
    19.             {  
    20.                 scanf("%d",&k);  
    21.                 val[i]+=pow2[k];  
    22.             }  
    23.         }  
    24.         total=pow2[n]-1;  
    25.         for(i=1;i<=total;i++)  
    26.         {  
    27.             c[i]=0;  
    28.             for(j=0;j<n;j++)  
    29.                if(i&pow2[j])  
    30.                  c[i]|=val[j];  
    31.         }  
    32.         for(i=1;i<=total;i++)  
    33.         {  
    34.             dp[i]=0;  
    35.             for(k=i;k>0;k=(k-1)&i)  
    36.                if(c[k]==total)  
    37.                  dp[i]=max(dp[i],dp[i^k]+1);  
    38.         }  
    39.         printf("Case %d: %d\n",++t,dp[total]);  
    40.     }  
    41.   

Hackers’ Crackdown - UVa11825 状压dp