陈老师的多校联合20140811||HDU 3236 ||2009年武汉站G题 01背包问题
来源:互联网 发布:做网络销售公司好做吗 编辑:程序博客网 时间:2024/04/30 07:02
http://acm.hdu.edu.cn/showproblem.php?pid=3236
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?
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 43 10 12 10 05 100 05 80 03 2 43 10 12 10 05 100 05 80 10 0 0
Sample Output
Case 1: 120Case 2: 100
解题思路:dp[i][j][k][0,1]表示前i物品已经选了,第一张优惠劵使用j元钱,第二张优惠劵使用了k元钱,一次免费的权利是否使用的状态下能得到的最大快乐值。用到滚动数组,空间太大。(代码借鉴)
#include<stdio.h>#include<iostream>#include<string.h>#define inff 0x3fffffffusing namespace std;int v1,v2,n;int p[310],h[310],s[310];int dp[2][510][55][2];//滚动数组优化空间int main(){ int i,j,k; int cas=1; while(scanf("%d%d%d",&v1,&v2,&n)&&v1+v2+n) { for(i=1; i<=n; i++) scanf("%d%d%d",&p[i],&h[i],&s[i]); for(i=0; i<=1; i++) //初始化 for(j=0; j<=v1; j++) for(k=0; k<=v2; k++) dp[i][j][k][0]=dp[i][j][k][1]=-inff; dp[0][0][0][0]=0; int t=1; for(i=1; i<=n; i++) { for(j=0; j<=v1; j++) { for(k=0; k<=v2; k++) { if(s[i]==0)//不买这个商品 { dp[t][j][k][0]=dp[1-t][j][k][0]; dp[t][j][k][1]=dp[1-t][j][k][1]; } if(j-p[i]>=0)//用第一种优惠劵买 { dp[t][j][k][0]=max(dp[t][j][k][0],dp[1-t][j-p[i]][k][0]+h[i]); dp[t][j][k][1]=max(dp[t][j][k][1],dp[1-t][j-p[i]][k][1]+h[i]); } if(k-p[i]>=0)//用第二种优惠劵买 { 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-p[i]][1]+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(j=0; j<=v1; j++) for(k=0; k<=v2; k++) dp[t][j][k][0]=dp[t][j][k][1]=-inff; } int ans=-inff; for(i=0; i<=v1; i++) for(j=0; j<=v2; j++) { ans=max(ans,dp[1-t][i][j][0]); ans=max(ans,dp[1-t][i][j][1]); } printf("Case %d: %d\n",cas++,max(-1,ans)); puts(""); } return 0;}
0 0
- 陈老师的多校联合20140811||HDU 3236 ||2009年武汉站G题 01背包问题
- 陈老师的多校联合2009年武汉站D题 ||hdu 3033
- 陈老师的多校联合2010811c题||09年武汉站||hdu3232
- 陈老师的多校联合 I题 二分
- 陈老师的多校联合 20140809 C题
- 陈老师的多校联合 20140808 E题
- 陈老师的多校联合 20140809
- 陈老师的多校联合20180808 map映射字符串
- 陈老师的多校联合20140815 ||uvalive 5881 区间处理
- 陈老师的多校联合20140816||spoj10237 bfs
- 陈老师的多校联合20140818||Uvalive 5683 模拟
- 陈老师的多校联合 D题 字符串处理起来挺麻烦
- 陈老师的多校联合20140816A题||spoj 10228 动态规划
- UVALive 5691 二分图匹配||陈老师的多校联合20140818 J题
- 陈老师的多校联合20140816||SPOJ AMR11B 三角形的有向面积
- hdu 4649 Professor Tian 多校联合训练的题
- hdu 01 背包问题
- 2016年武汉科技大学邀请赛网络赛 G题
- 了,只这一项,就能让他收回相当啡
- maximo上传文件中带有特殊字符附件设置
- Jersey框架搭建Rest服务
- spring动态创建,加载,使用多数据源
- How to run cpp-tests on iOS
- 陈老师的多校联合20140811||HDU 3236 ||2009年武汉站G题 01背包问题
- 一群人正说笑呢,忱
- 数据库设计准则(第一、第二、第三范式说明)
- 优秀程序员无他-善假于物也
- HDOJ 题目1849Rabbit and Grass(nim博弈)
- How to run cpp-tests on Mac OSX
- hdu 2089 不要62 (数位dp)
- SYSTEM权限引发的系列问题
- c#使用DotNetZip封装类操作zip文件(创建/读取/更新)实例