BZOJ1688|二进制枚举子集| 状态压缩DP

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 1

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

Sample Output 1

5

OUTPUT DETAILS:

If FJ milks cows 1, 2, 3, 5, and 6, then the milk will have only two
diseases (#1 and #2), which is no greater than K (2).

Source

[BZOJ1688][Usaco2005 Open]

网上找的代码,已经加上详细注解,明天补题写一遍

二进制枚举的做法:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,d,k;
int N[1000+5];

//统计有多少个1 
bool judge(int x)
{
    int c=0;
    while(x)
    {
        c++;              // 将x转化为2进制,看含有的1的个数。
        x&=(x-1);         //将最低的为1的位变成0
    }
    if(c<=k)              //限制个数
        return 1;
    else
        return 0;
}

int main()
{
    int s,t,total=0;
    scanf("%d%d%d",&n,&d,&k);
    for(int i=0; i<n; i++)
    {
        cin>>s;
        for(int j=0; j<s; j++)
        {
            cin>>t;
            N[i]|=1<<(t-1); //1<<t-1(1的二进制数整体向左移t-1位)
            //一起把二进制数的位数对应着来看,这两个数在这一位上有1的结果就是1,否则是0
        }
    }
    for(int i=0; i<(1<<d); i++)  //i<(1<<d)是当i不小于(1左移d位的数)时终止循环,枚举各子集对应的编码0,1,2,..2^d-1
    {
        if(judge(i)) //判断 患病子集为i 是否满足<d种 
        {
            int f = 0;
            for(int j=0; j<n; j++)
            {
            	//枚举n头牛 如果当前牛患病的个数小于 当前的i(患病子集),说明满足条件 ; 不满足就不要这头牛
                if((N[j]|i)==i) f++;  //对应N[j]与i的并集与i相等,说明N[j]是它的子集
            }
            if(f>total) //更新最大值 
                total=f;
        }
    }
    cout<<total<<endl;
    return 0;
}

状态压缩DP的做法:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1005],n,d,k,ans=0,ma=0,f[1<<16]; 


bool check(int x)
{
    int cnt=0;
    //枚举d位 (患病状态:1为患病 0为不换病)
    for (int i=1;i<=d;i++)
        {
            if (((1<<i-1)&x)>0) cnt++; //判断1~d各个位是否等于1,等于1 cnt就++ (表示患病数+1) 
            if (cnt>k) return false; //比k种病多 就返回 false(表示该方案不可行) 
        }
    return true;
}

int main()
{
    scanf("%d%d%d",&n,&d,&k);
    for (int i=1,x,y;i<=n;i++)
     {
        scanf("%d",&x);
        for (int j=1;j<=x;j++)
        {
            scanf("%d",&y);
            a[i]=a[i]|(1<<(y-1)); //存储状态:患病方案(10010 表示第2/5患病 1/3/4不换病)
        }
    }
    for (int i=1;i<=n;i++) //枚举n头牛  a[i]已表示这头牛的患病状态 
        for (int j=(1<<d);j>=1;j--) //枚举1<<d种患病状态 
            f[a[i]|j]=max(f[a[i]|j],f[j]+1); //a[i] | j 理解为:n头牛累加的患病总和(d种患病 和 n头牛患病状态 作或操作 就是求集合的并) 
            
    //筛选患病总数小于k的方案 1<<d个(从n头牛中选择i个)  判断每个(选牛)集合中满足条件(患病总数小于k)的集合  
    for (int i=1;i<=(1<<d);i++)
        if (check(i))
           ans=max(ans,f[i]);
    printf("%d",ans);
    return 0;
}
posted @ 2019-04-20 21:21  fishers  阅读(361)  评论(0编辑  收藏  举报