1688: [Usaco2005 Open]Disease Manangement 疾病管理

Description

Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Farmer John would like to milk as many of his N (1 <= N <= 1,000) cows as possible. If the milked cows carry more than K (1 <= K <= D) different diseases among them, then the milk will be too contaminated and will have to be discarded in its entirety. Please help determine the largest number of cows FJ can milk without having to discard the milk.

Input

* Line 1: Three space-separated integers: N, D, and K * Lines 2..N+1: Line i+1 describes the diseases of cow i with a list of 1 or more space-separated integers. The first integer, d_i, is the count of cow i's diseases; the next d_i integers enumerate the actual diseases. Of course, the list is empty if d_i is 0. 有N头牛,它们可能患有D种病,现在从这些牛中选出若干头来,但选出来的牛患病的集合中不过超过K种病.

Output

* Line 1: M, the maximum number of cows which can be milked.

Sample Input

6 3 2
0---------第一头牛患0种病
1 1------第二头牛患一种病,为第一种病.
1 2
1 3
2 2 1
2 2 1

Sample Output

5
 
 
 
状压DP,以前不是很懂,不过仔细的搞了搞这道题后有了很大收获,状压DP实际上很大一部分都是用一个二进制数来表示一种状态,1表示第n个值选了,0表示没选;
然后就涉及到了位运算。。。
我简单的说一下在我的代码里用到的几种位运算:
1<<n;表示在一个二进制数中1向左移n位,比如:1<<3,二进制位1000;
1<<n-1;1<<3-1=111;
x&y;表示x,y转化为二进制后每一位比较,如果都是1,就是1,否则是0,例如:0000 0000 0000 0100 &0000 0000 0000 0111= 0000 0000 0000 0100
x|y;表示x,y转化为二进制后每一位比较,只要某位是1,则为1,例如 0010 1010 | 0001 0101=0011 1111
然后状压DP具体的细节在代码里有注释。。。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #define maxn 50010
11 #define ll long long
12 #define inf 600000000
13 using namespace std;
14 int dis[20],f[1<<15],a[maxn];
15 int main(){
16     int n,d,k;
17     dis[0]=1;for(int i=1;i<=15;i++)dis[i]=(dis[i-1]<<1);//15位每一位代表一种疾病 
18     scanf("%d%d%d",&n,&d,&k);
19     int ed=(1<<d)-1;//11111111111 
20     for(int i=1,x;i<=n;i++){
21         scanf("%d",&x);
22         for(int j=0,y;j<x;j++){
23             scanf("%d",&y);
24             a[i]+=dis[y-1];//00010101类型,1为有第几种疾病 
25         }
26     }
27     for(int i=1;i<=n;i++)
28         for(int j=ed;j>=0;j--)//对于一头牛要加入,那么当前状态必须没有加入过这头牛,而当前状态与比他小的状态有关,所以要从大到小加 
29             f[j|a[i]]=max(f[j|a[i]],f[j]+1);//对于每种状态加上这头牛或者不加 
30     int flag=0,ans=0;
31     for(int i=0;i<=ed;i++){
32         int tot=0;
33         for(int j=1;j<=d;j++){
34             if(dis[j-1]&i){
35                 tot++;//检查每一种状态是否符合规定 
36                 if(tot>k)flag=1;
37             }
38         }
39         if(flag==0)ans=max(ans,f[i]);
40         flag=0; 
41     }
42     printf("%d",ans);
43     return 0;
44 }
View Code

 

posted @ 2015-10-05 20:28  HTWX  阅读(221)  评论(0编辑  收藏  举报