hdu 3182 Hamburger Magi 状压

Hamburger Magi

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 241    Accepted Submission(s): 83

Problem Description

In the mysterious forest, there is a group of Magi. Most of them like to eat human beings, so they are called “The Ogre Magi”, but there is an special one whose favorite food is hamburger, having been jeered by the others as “The Hamburger Magi”.

Let’s give The Hamburger Magi a nickname “HamMagi”, HamMagi don’t only love to eat but also to make hamburgers, he makes N hamburgers, and he gives these each hamburger a value as Vi, and each will cost him Ei energy, (He can use in total M energy each day).
In addition, some hamburgers can’t be made directly, for example, HamMagi can make a “Big Mac” only if “New Orleams roasted burger combo” and “Mexican twister combo” are all already made. Of course, he will only make each kind of hamburger once within a single
day. Now he wants to know the maximal total value he can get after the whole day’s hard work, but he is too tired so this is your task now!

Input

The first line consists of an integer C(C<=50), indicating the number of test cases.

The first line of each case consists of two integers N,E(1<=N<=15,0<=E<=100) , indicating there are N kinds of hamburgers can be made and the initial energy he has.

The second line of each case contains N integers V1,V2…VN, (Vi<=1000)indicating the value of each kind of hamburger.

The third line of each case contains N integers E1,E2…EN, (Ei<=100)indicating the energy each kind of hamburger cost.

Then N lines follow, each line starts with an integer Qi, then Qi integers follow, indicating the hamburgers that making ith hamburger needs.

Output

For each line, output an integer indicating the maximum total value HamMagi can get.

Sample Input

1
4 90
243 464 307 298
79 58 0 72
3 2 3 4
2 1 4
1 1
0

Sample Output

298

题意

输入n e

n代表n个汉堡,  e代表最大负重,每个汉堡只能拿一次

接下来一行四个数表示 n个汉堡的价值

再一行表示n个汉堡的花费

再接下来n行表示 第i汉堡如果要的话 需要先拿哪几个

如案例  3 2 3 4 ,表示要拿  第一个汉堡要先拿 编号2 3 4 这三个汉堡

思路

历遍所有状态,求最大值

#include<stdio.h>
int nd[20],vi[20],ci[20];
int main()
{
    int t,n,c,i,j;
    int tn,tem,tt;
    int lim,ans,flag,allc,allv;
    int zuiduo,dangqian,youbian;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&c);
        for(i=0;i<n;i++)
            scanf("%d",&vi[i]);
        for(i=0;i<n;i++)
            scanf("%d",&ci[i]);

        for(i=0;i<n;i++)
        {
            tem=0;
            scanf("%d",&tn);
            while(tn--)
            {
                scanf("%d",&tt);
                tem|=(1<<(tt-1));
            }
            nd[i]=tem;
        }

        lim=1<<n;
        ans=0;
        for(i=0;i<lim;i++)//zhuangtai
        {
            allv=0;
            allc=0;
            zuiduo=15;
            dangqian=0;
            while(zuiduo--)
            {
                youbian=0;
                for(j=0;j<n;j++)//meigewei
                {
                    if((i>>j)&1&&((dangqian>>j)&1)==0)//xuyao
                    {
                        if((dangqian&nd[j])==nd[j])//条件符合
                        {
                            allv+=vi[j];
                            allc+=ci[j];
                            youbian=1;
                            dangqian|=1<<j;
                        }
                    }
                }
                if(youbian==0)
                    break;
            }
            if(allc>c||allv<ans||dangqian!=i)
                continue;
            ans=allv;
        }
        printf("%d\n",ans);

    }
    return 0;
}
时间: 2024-10-03 11:34:05

hdu 3182 Hamburger Magi 状压的相关文章

HDU 3182 Hamburger Magi(状压dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3182 Problem Description In the mysterious forest, there is a group of Magi. Most of them like to eat human beings, so they are called "The Ogre Magi", but there is an special one whose favorite foo

HDU 3182 Hamburger Magi

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3182 题意:有n个汉堡,每个汉堡有一个价值和做汉堡需要的体力,并且做有些汉堡前需要先做别的汉堡,问最多能做出多大价值 每个汉堡只能做一次并且有先后顺序,所以需要状压dp而不能直接暴力判断 做汉堡的前置关系可以用状压来简化判断 还可以预处理出每个状态需要花费的体力 #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4924 Football Manager(状压DP)

题目连接 : http://acm.hdu.edu.cn/showproblem.php?pid=4924 题意 : n(<=20)个人选出11个人作为首发组成a-b-c阵容,每个人都有自己擅长的位置,并且每个人和其他人会有单向的厌恶和喜欢关系,每个人对于自己擅长的位置都有两个值CA和PA,有喜欢或者厌恶关系的两个人在一起也会影响整个首发的CA总值,要求选出一套阵容使得CA最大,或者CA一样的情况下PA最大. 思路 : 状压搞,dp[s]s的二进制表示20个人中选了那几个人,然后规定选进来的顺序

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 3247 AC自动+状压dp+bfs处理

Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 2382    Accepted Submission(s): 750 Problem Description Great! Your new software is almost finished! The only thing left to

HDU 1074 Doing Homework(状压DP)

Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will r

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

HDU 4909 String 统计+状压

因为连续异或满足区间减法性质,所以可以状压之后用异或来判断是否为符合条件的单词并且存储次数 一开始用map,一直超时.虽然直接用开1<<26的数组内存存的下,但是memset的时间肯定会超,但是只要在每次循环之后把加过的值减掉就可以绕过memset了. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits>

HDU 5765 Bonds(状压DP)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不同的连通块,对于每条边,求出两边的联通块的划分方案数,就是对于该点的答案. [代码] #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int n,m,T,Cas=1