Get Sauce(状压DP)

描述

In order to celebrate the 8th anniversary of ZOJ, LCLL goes to a sauce factory to "Get Sauce". The factory has N kinds of materials. If we combine some of them, we will get a bottle of sauce. LCLL is a sauce genius, he knows about M ways to make the sauce with these materials. Now LCLL wants to get as many bottles of sauce as possible, but he can use each kind of material only once. How many bottles of sauce will LCLL take home at most?

输入

The input file will contain multiple test cases. Each case contains two integers N and M(0 <=N <= 16, 0<= M <=50,000), then there are M lines, each line describe a way to make sauce like this: K a1,a2...aK where K(1<=K<=N) is the number of kinds of materials, ai is a number between[1,N] represents a kind of material this way needs.

Process to the end-of-file.

输出

For each test case print a single line that contains the number of the bottles of sauce LCLL will get at most.

样例输入

5 3
2 1 2
2 2 3
2 3 4

5 2
1 1
4 1 2 3 4

样例输出

2
1

题目大意:

现在有n种材料(每种只有一个),m种方案,每种方案需要若干种材料,求最多能 完成的方案数。

#include <bits/stdc++.h>
using namespace std;
int dp[1<<16];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof dp);
        int S=(1<<n)-1;
        for(int i=1,k;i<=m;i++)
        {
            scanf("%d",&k);
            int s=0;
            for(int j=1,x;j<=k;j++)
                scanf("%d",&x),s|=(1<<(x-1));
            int res=S^s;
            dp[s]=max(dp[s],1);
            for(int j=res;j;j=(j-1)&res)///遍历res子集
                if(dp[j])
                    dp[s|j]=max(dp[s|j],dp[j]+1);
        }
        int ans=0;
        for(int i=0;i<=S;i++)
            ans=max(ans,dp[i]);
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zdragon1104/p/9499691.html