HDU 3236 Gift Hunting (01背包)

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

Main idea & Solution
你要给你 朋友买礼物,有n个礼物可供选择,每个礼物有{价格,幸福值,是否必买}三个属性,你有两张超市发的代金券,不能将两张代金券并成一张"大代金券"使用,且不能折现。特别的,你拥有一次免费获得一个礼物的机会。
问:是否能满足女友要求,用两张代金券和一次免费机会将必买的礼物买下。若能,则求出最大总幸福值,若不能则输出-1

定义 d p [ j ] [ k ] [ s ] dp[j][k][s] dp[j][k][s]: 考虑前 i 个礼物的情况下,第一张代金券使用了j RMB,第二张用了 k RMB,免费机会使用情况为 s (0/1) 所能获得的最大幸福值(礼物维度滚动优化)
注意到这题还存在非法的情况,即用现有条件无法将当前所有必买的礼物买下
这种情况只会出现在当前物品必买时,具体处理方法见代码

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define endl '\n'
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MX = 500 + 7;

struct node{
    int p,h,s;
}a[MX];
int dp[MX][55][2];
int tmp[305], sum[305];
int main(){
    int v1,v2,n;
    int cas = 0;
    while(scanf("%d%d%d",&v1,&v2,&n)){
        clr(dp,0);//前0个物品的所有状态都合法
        if(v1 + v2 + n == 0) break;
        for(int i = 1;i <= n;++i){
            scanf("%d%d%d",&a[i].p,&a[i].h,&a[i].s);
        }
        for(int i = 1;i <= n;++i){
            for(int j = v1;j >= 0;--j){
                for(int k = v2;k >= 0;--k){
                    if(!a[i].s){//非必买
                        if(dp[j][k][1] != -1){
                            //先计算使用了免费券的情况
                            //因为要采用上一个物品的dp[j][k][0]

                            //白嫖
                            if(dp[j][k][0] != -1) dp[j][k][1] = max(dp[j][k][1],dp[j][k][0] + a[i].h);

                            //两种用券方法
                            if(j >= a[i].p && dp[j-a[i].p][k][1] != -1) dp[j][k][1] = max(dp[j][k][1],dp[j-a[i].p][k][1] + a[i].h);
                            if(k >= a[i].p && dp[j][k-a[i].p][1] != -1) dp[j][k][1] = max(dp[j][k][1],dp[j][k-a[i].p][1] + a[i].h);
                        }
                        if(dp[j][k][0] != -1) {
                            //两种用券方法
                            if(j >= a[i].p && dp[j-a[i].p][k][0] != -1) dp[j][k][0] = max(dp[j][k][0],dp[j-a[i].p][k][0] + a[i].h);
                            if(k >= a[i].p && dp[j][k-a[i].p][0] != -1) dp[j][k][0] = max(dp[j][k][0],dp[j][k-a[i].p][0] + a[i].h);
                        }
                    } else{//必买
                        if(dp[j][k][1] != -1){
                            //因为是必买所以不能考虑该物品不买的情况
                            int tmp1 = -1, tmp2 = -1;
                            if(j >= a[i].p && dp[j-a[i].p][k][1] != -1) tmp1 = dp[j-a[i].p][k][1] + a[i].h;
                            if(k >= a[i].p && dp[j][k-a[i].p][1] != -1) tmp2 = dp[j][k-a[i].p][1] + a[i].h;
                            //判断非法状态
                            dp[j][k][1] = max(tmp1,tmp2);

                            //白嫖
                            if(dp[j][k][0] != -1) dp[j][k][1] = max(dp[j][k][1], dp[j][k][0] + a[i].h);
                        }
                        if(dp[j][k][0] != -1){
                            int tmp1 = -1, tmp2 = -1;
                            if(j >= a[i].p && dp[j-a[i].p][k][0] != -1) tmp1 = dp[j-a[i].p][k][0] + a[i].h;
                            if(k >= a[i].p && dp[j][k-a[i].p][0] != -1) tmp2 = dp[j][k-a[i].p][0] + a[i].h;
                            //判断非法状态
                            dp[j][k][0] = max(tmp1,tmp2);
                        }
                    }
                }
            }
        }
        printf("Case %d: ",++cas);
        int res = -1;
        for(int j = 0;j <= v1;++j){
            for(int k = 0;k <= v2;++k){
                res = max(res,max(dp[j][k][1],dp[j][k][0]));
            }
        }
        printf("%d\n\n", res);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值