HDU3182 Hamburger Magi(状态压缩)

Hamburger Magi

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 462    Accepted Submission(s): 146


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
 

Source
HDU 2009-10 Programming Contest
题意:有N个汉堡包要做和拥有E能量,每个汉堡包有不同的价值和需要损耗能量,并且每个汉堡包都要前提条件,要求先做完某些汉堡包之后才能做,每个汉堡包只能做一次。
思路:汉堡包最多有15个,可以用状态压缩表示当前状态,dp[i]表示当状态为i时能获得的最大价值,对于每个汉堡包只做一次,并且用状态判断是否满足前提条件,记录每种状态下剩余的能量, 判断能量是否满足。
#include 
#include 
#include 
#define maxn (1<<15)+5
using namespace std;
int dp[maxn], energy[maxn];
int v[20], e[20], need[105];
int main()
{
    int C, N, E, i, j, k, T, u;
    int ans, tmp;
    scanf("%d", &C);
    while(C--)
    {
        scanf("%d %d", &N, &E);
        for(i = 0;i < N;i++) scanf("%d", &v[i]);
        for(i = 0;i < N;i++) scanf("%d", &e[i]);
        for(i = 0;i < N;i++){
            scanf("%d", &T);
            tmp = 0;
            while(T--){
                scanf("%d", &u);
                if(u>0){
                    u--;
                    tmp |= (1<= e[j]){
                    dp[i|(1<


你可能感兴趣的:(HDU,动态规划,状态压缩)