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
#include
#include
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>s;
        for(int j=0; j>t;
            N[i]|=1<<(t-1); //1<total) //更新最大值 
                total=f;
        }
    }
    cout<

状态压缩DP的做法:

#include
#include
#include
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<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<=1;j--) //枚举1<

你可能感兴趣的:(BZOJ1688|二进制枚举子集| 状态压缩DP)