Hamburger Magi
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
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!
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种汉堡,每种汉堡可以产生 vi 的价值,需要消耗 ei 的能量,且要做第i种汉堡,必须先做好其他几种汉堡(已知),现在总共有E能量,问能产生的最大价值。
思路:状压DP
设dp[i]表示在第 i 种状态下能产生的最大价值。
若可以做n种汉堡,就会有2^n种状态:
以n=3为例:000 001 010 011 100 101 110 111 (共2^3=8种)
0表示这种汉堡没做,1表示这种汉堡已经做了。
详细操作见代码及备注。
/*状压DP基本操作
( 1 << (i-1) ) & x :判断数x在二进制下第i位是不是为1 ;
x |= ( 1 << (i-1) ) :把数x在二进制下的第i位更改为1 ;
x & ( x << 1 ) :判断是否相邻 ;
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 1e9+7;
int n,e;
int val[20]; //每种汉堡的价值
int cost[20]; //每种汉堡的花费
int a[20][20]; //每种汉堡做的时候需要事先做好哪几种汉堡,其中a[i][0]表示做第i种汉堡时需要事先做好的汉堡总数
int restm[1<<17]; //restm[state]表示在state状态下还剩余的能量
int dp[1<<17]; //dp[state]表示state状态下能产生的最大价值
int judge(int pos,int state)
{
for(int i=1;i<=a[pos][0];i++)
{
//检查是否满足做某汉堡包时题目给出的要在做他之前做的汉堡包都已经做了
if(!( state&(1<<(a[pos][i]-1) ) ) )
return 0;
}
return 1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&e);
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
for(int i=1;i<=n;i++)
scanf("%d",&cost[i]);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i][0]);
for(int j=1;j<=a[i][0];j++)
scanf("%d",&a[i][j]);
}
for(int i=0;i<(1<<n);i++)
{
dp[i]=-INF;
restm[i]=0;
}
dp[0]=0;
restm[0]=e;
int ans=0;
for(int i=0;i<(1<<n);i++)
{
for(int j=1;j<=n;j++)
{
//如果第 j 个汉堡已经做过了的话就不再更新
if(i&1<<(j-1))
continue;
//做第 j 个汉堡
int now=i|(1<<(j-1));
if(dp[now]<dp[i]+val[j]&&judge(j,i)&&restm[i]>=cost[j])
{
dp[now] = dp[i] + val[j];
restm[now] = restm[i] - cost[j];
ans=max(ans,dp[now]);
}
}
}
printf("%d\n",ans);
}
return 0;
}