Hamburger Magi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 509 Accepted Submission(s): 163
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!
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
做的第二道状态压缩dp。
状态压缩dp。虽然感觉不难,但是没能独立写出来。自己的代码估计是在关联的汉堡的处理上存在问题。
看了题解,很高兴基本套路没错,状态转移有问题(思路有问题)。
最后改为从后往前推,初始dp数组为-1,dp[0]=0,这样往后推,那么假如现在是dp[10100],处理到第5个汉堡,而第5个汉堡跟第1个和第3个相关联,那么在没有制作1和3时dp[10100]为-1,而为-1直接跳过,这样很好地处理了关联的问题。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; struct Node { int value; int cost; int cnt; int rela[20]; } ham[20]; struct Node1 { int value; int cost; }; Node1 dp[1<<15]; int main() { int t,kind,ener; scanf("%d",&t); while(t--) { memset(dp,-1,sizeof(dp)); scanf("%d%d",&kind,&ener); for(int i=1; i<=kind; i++) scanf("%d",&ham[i].value); for(int i=1; i<=kind; i++) scanf("%d",&ham[i].cost); for(int i=1; i<=kind; i++) { scanf("%d",&ham[i].cnt); for(int j=0; j<ham[i].cnt; j++) scanf("%d",&ham[i].rela[j]); } int en=1<<kind; int ans=0; dp[0].cost=dp[0].value=0; for(int j=0; j<en; j++) { if(dp[j].value==-1) continue; for(int i=1; i<=kind; i++) { int tem=1<<(kind-i); if(j&tem) continue; int flag=1; for(int k=0; k<ham[i].cnt; k++) { int temp=1<<(kind-ham[i].rela[k]); if(!(temp&j)) { flag=0; break; } } if(flag) { dp[j+tem].value=dp[j].value+ham[i].value; dp[j+tem].cost=dp[j].cost+ham[i].cost; } } } for(int i=0; i<en; i++) { if(dp[i].cost>ener) continue; if(ans<dp[i].value) ans=dp[i].value; } printf("%d\n",ans); } return 0; }