HDU3236:Gift Hunting(类01背包加强版)
2017-02-04 18:43
316 查看
reference:http://blog.csdn.net/madaidao/article/details/35794835
题意:两张优惠券,不能合在一起使用,n件物品,每件三个属性:价格,幸福值,是否必须购买。求用完代金券获得的最大幸福值,若必买的商品不能买完(即使用了免费挑选的机会)输出-1.
思路:双背包+部分物品必选+可无代价选择任意一个。双背包:增加一维dp[i][j] = max(dp[i][j], dp[i-c[k]][j]+v[k], dp[i][j-c[k]]+v[k])。部分物品必选:如果c[k]不必选,dp[i][j] = 上一状态的dp[i][j],否则dp[i][j] = -∞,再执行前面的方程。 可无代价选择任意一个:再增加一维dp[i][j][k],k=1或0代表是否已经选了。
4000
7 口碑商家客流量预测大赛》 |
Gift HuntingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1791 Accepted Submission(s): 593 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. Sample Input 3 2 4 3 10 1 2 10 0 5 100 0 5 80 0 3 2 4 3 10 1 2 10 0 5 100 0 5 80 1 0 0 0 Sample Output Case 1: 120 Case 2: 100 Source 2009 Asia Wuhan Regional Contest Hosted by Wuhan University |
思路:双背包+部分物品必选+可无代价选择任意一个。双背包:增加一维dp[i][j] = max(dp[i][j], dp[i-c[k]][j]+v[k], dp[i][j-c[k]]+v[k])。部分物品必选:如果c[k]不必选,dp[i][j] = 上一状态的dp[i][j],否则dp[i][j] = -∞,再执行前面的方程。 可无代价选择任意一个:再增加一维dp[i][j][k],k=1或0代表是否已经选了。
# include <stdio.h> # include <string.h> # include <algorithm> # define INF 0x3f3f3f3f using namespace std; int dp[2][501][51][2], p[301], h[301], s[301]; int main() { int v1, v2, n, ans, cas=1; while(~scanf("%d%d%d",&v1,&v2,&n),v1+v2+n) { ans = -1; for(int i=0; i<n; ++i) scanf("%d%d%d",&p[i],&h[i],&s[i]); for(int i=0; i<=v1; ++i) for(int j=0; j<=v2; ++j) dp[0][i][j][0] = dp[1][i][j][1] = dp[1][i][j][0] = dp[0][i][j][1] = -INF; dp[0][0][0][0] = 0; int t = 1; for(int i=0; i<n; ++i) { for(int j=0; j<=v1; ++j) { for(int k=0; k<=v2; ++k) { if(!s[i]) { dp[t][j][k][1] = dp[1-t][j][k][1]; dp[t][j][k][0] = dp[1-t][j][k][0]; } if(j>=p[i]) { dp[t][j][k][1] = max(dp[t][j][k][1], dp[1-t][j-p[i]][k][1]+h[i]); dp[t][j][k][0] = max(dp[t][j][k][0], dp[1-t][j-p[i]][k][0]+h[i]); } if(k>=p[i]) { dp[t][j][k][1] = max(dp[t][j][k][1], dp[1-t][j][k-p[i]][1]+h[i]); dp[t][j][k][0] = max(dp[t][j][k][0], dp[1-t][j][k-p[i]][0]+h[i]); } dp[t][j][k][1] = max(dp[t][j][k][1], dp[1-t][j][k][0]+h[i]); } } t = 1-t; for(int i=0; i<=v1; ++i) for(int j=0; j<=v2; ++j) dp[t][i][j][0] = dp[t][i][j][1] = -INF; } t = 1-t; for(int i=0; i<=v1; ++i) for(int j=0; j<=v2; ++j) ans = max(ans, dp[t][i][j][1]); printf("Case %d: %d\n",cas++, ans); puts(""); } return 0; }
4000
相关文章推荐
- WZOJ 1304 01背包加强版(完全背包单调队列优化)
- 新年趣事之打牌----VIJOS_1071----01背包加强版
- 新年趣事之打牌----VIJOS_1071----01背包加强版
- uva10130 01背包加强版
- HDU 3236 Gift Hunting dp 背包
- 【POJ1014】Dividing 多重背包,二进制物品拆分转01背包
- 01背包问题-----回溯法的解决方案
- UVa 12563 - Jin Ge Jin Qu hao(类01背包,白书)
- BZOJ 4753: [Jsoi2016]最佳团体 树形背包 01分数规划
- hdu3466Proud Merchants (01背包,以q-p从小到大排序)
- HUD 5188(zhx and contest)有条件01背包
- hdu2602-01背包
- 01背包问题:初入动规坑
- 01背包 实例
- L3-001 凑零钱 (01背包,并按要求输出背包内的东西)
- hdu 2602 Bone Collector (01 背包问题)
- HDU 3466 Proud Merchants(01背包的应用)
- 类似于01背包问题 金明的预算
- nyoj 又见01背包
- HDU1171 Big Event in HDU(01背包的转化,多重背包)