HDU 3182 Hamburger Magi(状压DP解决有顺序背包问题)

 Hamburger Magi
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

01背包和拓扑排序结合。。。情况太多。。。就直接遍历所有情况了。。。->状压解决

位运算符号&不要手残写多了。。、

题意:
      一个人做汉堡包,每个汉堡包有自己的花费和价值,某些汉堡包必须是在其他的某些汉堡包已经做好了的前提下才能制作,给你这个人的初始钱数,问最大的价值是多少。

思路:

      比较简单的一个题目,首先我们开一个数组dp[i]表示i状态(状态压缩)时的最大价值,把每一个状态都用这n个汉堡包更新一下,还的开个数组money[i]表示的是i状态是的剩余钱数,更新的时候记住一点就是每个点只能用一次,也就是当前状态里如果有i这个点,那么i不能在来更新了。



#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
const int N = (1<<16)+10;
const int inf = -0x3f3f3f3f;
int  dp[N], money[N], limit[N][20], v[N], w[N];
bool judge(int s,int j)
{
    for(int i=1;i<=limit[j][0];i++)
    {
        if(!(s&(1<<(limit[j][i]-1)))) return 0;
    }
    return 1;
}




int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        memset(money,0,sizeof(money));
        for(int i=1;i<=n;i++)
            scanf("%d", &v[i]);
        for(int i=1;i<=n;i++)
            scanf("%d", &w[i]);
        for(int i=1;i<=n;i++)
        {
            scanf("%d", &limit[i][0]);
            for(int j=1;j<=limit[i][0];j++)
                scanf("%d",&limit[i][j]);
        }
        for(int i=1;i<(1<<n);i++)
            dp[i]=-100000000, money[i]=0;
        dp[0]=0,money[0]=m;
        int ans=0;
        for(int i=0;i<(1<<n);i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i&(1<<(j-1))) continue;
                int state=i|(1<<(j-1));
                if(dp[state]<dp[i]+v[j]&&money[i]>=w[j]&&judge(i,j))
                {
                    dp[state]=dp[i]+v[j];
                    money[state]=money[i]-w[j];
                    ans=max(ans,dp[state]);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值