HDU 3182 - Hamburger Magi - [状压DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3182
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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个汉堡包,编号1~N,做汉堡包的法师共有能量E;
每个汉堡包给出价值和需要消耗的能量,以及做这个汉堡包需要的前置汉堡包k1,k2, … , ki;
求在最多消耗能量E的情况下,做出来的汉堡最大价值和;
题解:
状态i表示哪些汉堡包做了,哪些汉堡包还没做;
dp[i].val:当前情况下,最大价值和;
dp[i].ene:当前情况下,消耗了多少能量;
AC代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define INF 0x3f3f3f3f 5 using namespace std; 6 int N,E; 7 struct Ham{ 8 int val,ene,pre; 9 }ham[16]; 10 struct DP{ 11 int val,ene; 12 }dp[1<<15]; 13 int main() 14 { 15 int t; 16 scanf("%d",&t); 17 while(t--) 18 { 19 memset(dp,-1,sizeof(dp)); 20 scanf("%d%d",&N,&E); 21 for(int i=1;i<=N;i++) scanf("%d",&ham[i].val); 22 for(int i=1;i<=N;i++) scanf("%d",&ham[i].ene); 23 for(int i=1,q;i<=N;i++) 24 { 25 scanf("%d",&q); 26 ham[i].pre=0; 27 for(int j=1,tmp;j<=q;j++) 28 { 29 scanf("%d",&tmp); 30 ham[i].pre|=1<<(tmp-1); 31 } 32 } 33 34 int ed_state=(1<<N)-1,ans=0; 35 dp[0].val=0, dp[0].ene=0; 36 for(int state=0;state<=ed_state;state++) 37 { 38 if(dp[state].val==-1) continue; 39 for(int i=1;i<=N;i++) 40 { 41 if( state&(1<<(i-1)) || (state&ham[i].pre) != ham[i].pre || dp[state].ene+ham[i].ene > E ) continue; 42 int next_state=state|(1<<(i-1)); 43 if(dp[state].val+ham[i].val > dp[next_state].val) 44 { 45 dp[next_state].val=dp[state].val+ham[i].val; 46 dp[next_state].ene=dp[state].ene+ham[i].ene; 47 if(dp[next_state].ene<=E) ans=max(dp[next_state].val,ans); 48 } 49 } 50 } 51 52 printf("%d\n",ans); 53 } 54 }
转载请注明出处:https://dilthey.cnblogs.com/