# HDU 3182 Hamburger Magi(状压DP)

115人阅读 评论(0)

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.

#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

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：3156次
• 积分：294
• 等级：
• 排名：千里之外
• 原创：26篇
• 转载：4篇
• 译文：0篇
• 评论：0条
文章分类
阅读排行
评论排行