zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    JAVA多线程2 锁
    IE8标准模式下VML不能显示问题
    JAVA多线程1
    JAVA判断32位还是64位,调用不同的DLL
    JNA调用DLL
    如何提高执行力
    httpClient多线程请求
    【NodeJS】安装
    [转载]一个项目涉及到的50个Sql语句(整理版)
    resultMap中的collection集合出现只能读取一条数据的解决方法
  • 原文地址:https://www.cnblogs.com/zdragon1104/p/9499691.html
Copyright © 2011-2022 走看看