HDU 3182 Hamburger Magi(状压DP)

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.

题意:

做汉堡吧!给出n个汉堡的价值和能量花费,以及其依赖关系(要做这个汉堡必须先完成其他一些汉堡),给出初始能量,求能做出汉堡的最大价值。

解题思路:

单纯的状压DP,三层注意循环的顺序。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[1<<15][105];                  //dp[i][j]表示 当前状态为i使用了j的能量所能获得的最大价值。
bool vis[1<<15][105];                //表示当前状态是否能够达到
int w[20],c[20],need[20][20];
int main()
{
        int T,n,maxv,nn;
        cin>>T;
        while(T--)
        {
                cin>>n>>maxv;
                for(int i=0;i<n;i++)
                        cin>>w[i];
                for(int i=0;i<n;i++)
                        cin>>c[i];
                for(int i=0;i<n;i++)
                {
                        cin>>need[i][0];
                        for(int j=1;j<=need[i][0];j++)
                                cin>>need[i][j];
                }
                int ans=0;
                memset(dp,0,sizeof dp);
                memset(vis,0,sizeof vis);
                int cnt=(1<<n)-1;
                vis[0][0]=1;                           
                for(int j=0;j<=maxv;j++)               //刷表
                        for(int k=0;k<=cnt;k++)
                        {
                                if(!vis[k][j])         //只用能够达到的状态
                                        continue;
                                for(int i=0;i<n;i++)   //每种状态尝试做新的汉堡
                                {
                                        if(k&(1<<i))
                                                continue;
                                        if(j+c[i]>maxv)
                                                continue;
                                        int flag=0;
                                        for(int t=1;t<=need[i][0];t++)
                                        {
                                                int id=1<<(need[i][t]-1);
                                                if((k&id)==0)
                                                {
                                                        flag=1;
                                                        break;
                                                }
                                        }
                                        if(flag)
                                                continue;
                                        dp[k|(1<<i)][j+c[i]]=max(dp[k|(1<<i)][j+c[i]],dp[k][j]+w[i]);
                                        vis[k|(1<<i)][j+c[i]]=1;
                                        ans=max(ans,dp[k|(1<<i)][j+c[i]]);       //找答案
                                }
                        }
                cout<<ans<<endl;
        }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值