Gift Hunting

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1418 Accepted Submission(s): 471

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

题意:

GG有两张奖券,面额分别为V1、V2,准备给MM买礼物。当天是MM生日,所以MM还可以免费得到一份礼物。

礼物有价格和幸福值两个参数,并且礼物分为特殊礼物和普通礼物,特殊礼物是一定要全部到手的。

两张奖券不可以合并使用,、一种礼物也只能买一次。

问MM最大幸福值是多少,如果特殊礼物不能全部得到MM会不高兴,幸福值为-1。

思路:先处理特殊的物品,看是不是可以买完,然后再买普通的物品

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef unsigned long long LL; typedef pair<int ,int >pr; const int Max = 110000; pr SG[350];//Special Gift pr NG[350];//Normal Gift int SN,NN;//Special num && Normal num int Dp[600][60][2];//Dp[i][j][k]表示在V1花费为i,V2花费为j时,状态为k是的幸福值k=1表示免费k=0表示没有免费的 int V1,V2,n; bool GSG()//处理Special Gift;
{
int total,pretotal;
for(int i=0; i<=V1; i++)//初始化
{
for(int j=0; j<=V2; j++)
{
for(int k=0; k<=1; k++)
{
Dp[i][j][k]=-1;
}
}
}
Dp[0][0][0]=0;
total=0;
for(int i=0; i<SN; i++)
{
pretotal = total;//pretotal记录处理第i个礼物时已经必须购买的物品的总价值
total+=SG[i].second;//购买完当前物品后的总价值
for(int s1=V1; s1>=0; s1--)
{
for(int s2=V2; s2>=0; s2--)
{
if(Dp[s1][s2][1]==pretotal)//为了防止东西漏买,所以只处理价值等于pretotal,因为只有这种情况之前的东西才能全买
{
if(s1+SG[i].first<=V1)//限制条件
{
Dp[s1+SG[i].first][s2][1]=total;
}
if(s2+SG[i].first<=V2)
{
Dp[s1][s2+SG[i].first][1]=total;
}
}
if(Dp[s1][s2][0]==pretotal)//同上
{
if(s1+SG[i].first<=V1)
{
Dp[s1+SG[i].first][s2][0]=total;
}
if(s2+SG[i].first<=V2)
{
Dp[s1][s2+SG[i].first][0]=total;
}
Dp[s1][s2][1]=total;//可以免费买;
}
}
}
}
bool flag=false;//判断是不是可以将特殊的物品买完
for(int i=0; i<=V1; i++)
{
for(int j=0; j<=V2; j++)
{
for(int k=0; k<=1; k++)
{
if(Dp[i][j][k]==total)//如果价值与总价值相等,说明已经买完所有必须买的物品,否则归为-1,使的买普通物品时是建立在买完特殊物品的基础上
{
flag=true;
}
else
{
Dp[i][j][k]=-1;
}
}
}
}
return flag;
}
int GNG()
{
for(int i=0; i<NN; i++)
{
for(int s1=V1; s1>=0; s1--)
{
for(int s2=V2; s2>=0; s2--)
{
for(int k=0; k<=1; k++)
{
if(Dp[s1][s2][k]!=-1)//判断是不是在买完特殊物品后再买的状态
{
if(s1+NG[i].first<=V1&&Dp[s1][s2][k]+NG[i].second>Dp[s1+NG[i].first][s2][k])
{
Dp[s1+NG[i].first][s2][k]=Dp[s1][s2][k]+NG[i].second;
}
if(s2+NG[i].first<=V2&&Dp[s1][s2][k]+NG[i].second>Dp[s1][s2+NG[i].first][k])
{
Dp[s1][s2+NG[i].first][k]=Dp[s1][s2][k]+NG[i].second;
}
}
}
if(Dp[s1][s2][0]!=-1&&Dp[s1][s2][1]<Dp[s1][s2][0]+NG[i].second)//免费的物品,选择最优
{
Dp[s1][s2][1]=Dp[s1][s2][0]+NG[i].second;
}
}
}
}
int ans=0;
for(int i=0; i<=V1; i++)
{
for(int j=0; j<=V2; j++)
{
for(int k=0; k<=1; k++)
{
ans=max(ans,Dp[i][j][k]);
}
}
}
return ans;//选择最大的幸福值
}
int main()
{
int w=1;
int h,s,p;
while(scanf("%d %d %d",&V1,&V2,&n))
{
if(V1==0&&V2==0&&n==0)
{
break;
}
SN=0;
NN=0;
for(int i=0; i<n; i++)
{
scanf("%d %d %d",&p,&h,&s);
if(s==1)
{
SG[SN].first=p;
SG[SN++].second=h;
}
else
{
NG[NN].first=p;
NG[NN++].second=h;
}
}
if(GSG())//如果可以将特殊物品买完
{
printf("Case %d: %d\n\n",w++,GNG());
}
else
{
printf("Case %d: -1\n\n",w++);
}
}
return 0; }

Gift Hunting(分组背包)的更多相关文章

  1. HDU 1712 ACboy needs your help(分组背包)

    题意:给你n的课程组,每个课程组有m个课程,每个课程有一个完成时间与价值.问在m天内每组课程组最多选择一个,这样可以得到的最大价值是多少 题解:分组背包,其实就是每个课程组进行01背包,再在课程组内部 ...

  2. Codeforces Round #383 (Div. 2) D 分组背包

    给出一群女孩的重量和颜值 和她们的朋友关系 现在有一个舞台 ab是朋友 bc是朋友 ac就是朋友 给出最大承重 可以邀请这些女孩来玩 对于每一个朋友团体 全邀请or邀请一个or不邀请 问能邀请的女孩的 ...

  3. HDU 3033 分组背包变形(每种至少一个)

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 1712 分组背包

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  6. HDU3535AreYouBusy[混合背包 分组背包]

    AreYouBusy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. POJ1837 Balance[分组背包]

    Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13717   Accepted: 8616 Descript ...

  8. Codevs1378选课[树形DP|两种做法(多叉转二叉|树形DP+分组背包)---(▼皿▼#)----^___^]

    题目描述 Description 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修 ...

  9. hdu1712 分组背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1712 题意:有n门课程,和m天时间,完成mp[i][j]得到的价值为第i行j列的数字,求最 ...

随机推荐

  1. J2EE MyBatis使用

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...

  2. C++Primer 第九章

    //1.vector:可变大小数组.支持快速随机访问,在尾部之外的位置插入或删除元素可能很慢.注意点:不要在vector中存放bool类型,vector<bool>并不是一个容器,其实现方 ...

  3. 网易免费邮件开启smtp教程

    网易免费邮件开启smtp教程     作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们在部署zabbix邮件报警中可能会用到163.com.那么我们如何去开启smtp服务呢? 1 ...

  4. 源码搭建LNMP

      源码安装LNMP 作者:尹正杰   版权声明:原创作品,谢绝转载!否则将追究法律责任.       欢迎加入:高级运维工程师之路 598432640 前言:非常简单的一个平台LNMP,在生产实际环 ...

  5. [原创]java WEB学习笔记76:Hibernate学习之路---Hibernate介绍,hibernate 环境的搭建

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. 。。。Spring框架总结(一)。。。

    Spring框架已经学习了两遍了,第一遍基本上忘得差不多了,现在开始复习第二遍的,也复习的差不多了,比之前懂了很多东西,今天就写下来,记录一下我滴小成果! 首先,在Spring框架中有两个重要的概念: ...

  7. session 和 cookie 的区别与联系

    1.创建一个新的Cookie Cookie cookie = new Cookie("username",name); 2.设置cookie在客户端上存活多久 cookie.set ...

  8. sql 存储过程参数是表类型,数据库中如何调用

    DECLARE @NEW_STUDENT as [CancelLendersContent] INSERT @NEW_STUDENT VALUES (0,0,0,'12345678912','张三', ...

  9. System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 未将对象引用设置为实例对象

    做项目的时候,System.Web.HttpContext.Current.Server.MapPath("~/upload/SH")   获取路径本来这个方法用的好好的 因为需要 ...

  10. oracle中的常用语句

    1:查看当前用户的缺省表空间 SELECT USERNAME, DEFAULT_TABLESPACE FROM USER_USERS; 2:查看当前用户的角色 SELECT * FROM USER_R ...