Gift Hunting
Time Limit: 6000/3000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 936 Accepted Submission(s): 306
Problem Description After winning two coupons for the largest shopping mart in your city, you can't wait inviting your girlfriend for gift hunting. Having inspected hundreds of kinds of souvenirs, toys and cosmetics, you finally narrowed down the candidate list to only n gifts, numbered 1 to n. Each gift has a happiness value that measures how happy your girlfriend would be, if you get this gift for her. Some of them are special - you must get it for your girlfriend (note that whether a gift is special has nothing to do with its happiness value).
Coupon 1 can be used to buy gifts with total price not greater than V1 (RMB). Like most other coupons, you can’t get any money back if the total price is strictly smaller than V1. Coupon 2 is almost the same, except that it’s worth V2. Coupons should be used separately. That means you cannot combine them into a super-coupon that’s worth V1+V2. You have to divide the gifts you choose into two part, one uses coupon 1, the other uses coupon 2.
It is your girlfriend's birthday today. According to the rules of the mart, she can take one (only one) gift for FREE! Here comes your challenge: how to make your girlfriend as happy as possible?
Input
There will be at most 20 test cases. Each case begins with 3 integers V1, V2 and n (1 <= V1 <= 500, 1 <= V2 <= 50, 1 <= n <= 300), the values of coupon 1 and coupon 2 respectively, and the number of candidate gifts. Each of the following n lines describes a gift with 3 integers: P, H and S, where P is the price, H is the happiness (1 <= P,H <= 1000), S=1 if and only if this is a special gift - you must buy it (or get it for free). Otherwise S=0. The last test case is followed by V1 = V2 = n = 0, which should not be processed.
Output
For each test case, print the case number and the maximal total happiness of your girlfriend. If you can't finish the task, i.e. you are not able to buy all special gifts even with the 1-FREE bonus, the happiness is -1 (negative happiness means she's unhappy). Print a blank line after the output of each test case.
思路:根据题目要求,从01背包的思路出发,不难想到这样的状态dp[w][x][y][z],意思是取到第w件物品,第一张消费券花了x,第二张消费卷花了y,取了z件免费物品时的最优值。然而题目中最大值有n = 300,v1 = 500, v2 = 50, z = 1,则空间复杂度为300*500*50*1,会MLE。从01背包的启示可以知道,可以进行降维,定义状态为dp[x][y][z],循环更新值的时候逆序即可。因为有些物品是必取的,所以初始化dp[x][y][z]为 -1,dp[0][0][0] = 0; 遇到必取的物品时,要用一个临时数组去找最优值,之后才拿这个数组去更新数组dp[][][],因为在多维背包了进行了降维处理,如果直接在dp[][][]上操作,会出现值被覆盖掉的情况。
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 struct node 9 { 10 int p,h,s; 11 }; 12 13 int dp[505][55][2]; 14 int temp[505][55][2]; 15 int v1,v2,n; 16 node gift[305]; 17 18 void init() 19 { 20 memset(dp,-1,sizeof(-1)); 21 dp[0][0][0] = 0; 22 } 23 24 int main() 25 { 26 int Case = 1; 27 while(~scanf("%d%d%d",&v1,&v2,&n)) 28 { 29 if(!v1 && !v2 && !n) break; 30 init(); 31 for(int i=1; i<=n; i++) 32 scanf("%d%d%d",&gift[i].p,&gift[i].h,&gift[i].s); 33 for(int i=1; i<=n; i++) 34 { 35 if(gift[i].s == 1) 36 { 37 memset(temp,-1,sizeof(temp)); 38 for(int g=1; g>=0; g--) 39 { 40 for(int j=v1; j>=0; j--) 41 { 42 for(int k=v2; k>=0; k--) 43 { 44 if(j >= gift[i].p && dp[j-gift[i].p][k][g] != -1) 45 temp[j][k][g] = max(temp[j][k][g],dp[j-gift[i].p][k][g]+gift[i].h); 46 if(k >= gift[i].p && dp[j][k-gift[i].p][g] != -1) 47 temp[j][k][g] = max(temp[j][k][g],dp[j][k-gift[i].p][g]+gift[i].h); 48 if(g && dp[j][k][0] != -1) 49 temp[j][k][1] = max(temp[j][k][1],dp[j][k][0]+gift[i].h); 50 } 51 } 52 } 53 for(int j=0; j<=v1; j++) 54 for(int k=0; k<=v2; k++) 55 for(int g=0; g<=1; g++) 56 dp[j][k][g] = temp[j][k][g]; 57 } 58 else 59 { 60 for(int g=1; g>=0; g--) 61 { 62 for(int j=v1; j>=0; j--) 63 { 64 for(int k=v2; k>=0; k--) 65 { 66 if(g && dp[j][k][0] != -1) 67 dp[j][k][1] = max(dp[j][k][1],dp[j][k][0]+gift[i].h); 68 if(j >= gift[i].p && dp[j-gift[i].p][k][g] != -1) 69 dp[j][k][g] = max(dp[j][k][g],dp[j-gift[i].p][k][g]+gift[i].h); 70 if(k >= gift[i].p && dp[j][k-gift[i].p][g] != -1) 71 dp[j][k][g] = max(dp[j][k][g],dp[j][k-gift[i].p][g]+gift[i].h); 72 } 73 } 74 } 75 } 76 } 77 int ans = -1; 78 for(int i=0; i<=v1; i++) 79 for(int j=0; j<=v2; j++) 80 for(int k=0; k<=1; k++) 81 ans = max(ans,dp[i][j][k]); 82 if(ans <= 0) 83 printf("Case %d: -1\n\n",Case++); 84 else 85 printf("Case %d: %d\n\n",Case++,ans); 86 } 87 return 0; 88 }