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. (Builder)创建者模式

    定义: 建造模式:将一个复杂对象的构建与他的表示相分离,使得同样的构建过程可以创建不同的表示. 适用性: 当流程算法可以固定几个步骤,步骤的算法步骤执行顺序固定,且制造的产品可以唯一确定,这时使用创建 ...

  2. Swift实战-小QQ(第2章):QQ侧滑菜单

    QQ侧滑实现架构:需要建立以下几个ViewController:1.XQBaseViewController 2.LeftViewController3.RightViewController4.Co ...

  3. Winform 窗口拖动

    把窗口边框去掉后,窗口拖动问题: private Point mouseOffset; //记录鼠标指针的坐标 private bool isMouseDown = false; //记录鼠标按键是否 ...

  4. Java基础(8):方法重载的4个依据与例子

    判断方法重载的依据: 1. 必须是在同一个类中 2. 方法名相同 3. 方法参数的个数.顺序或类型不同 4. 与方法的修饰符或返回值没有关系 运行结果:

  5. android与后台请求的例子

    public static ClientResponse SendClientRequest(List<BasicNameValuePair> params){ ClientRespons ...

  6. C++之路进阶——codevs1285(宠物收养所)

    1285 宠物收养所  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服 ...

  7. 夺命雷公狗---Thinkphp----6之管理员的增删改查之-未验证

    首先我们创建多一个控制器UserController.class.php,主要用于管理员的增删改查操作: 代码如下所示: <?php namespace Admin\Controller; us ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON DirectShow (Delphi Prism)

    zw版[转发·台湾nvp系列Delphi例程]HALCON DirectShow (Delphi Prism) namespace DirectShow_Prism;interfaceuses Sys ...

  9. SSAS更改默认端口号,使用非默认端口号的时候Olap连接字符串的格式

    Sql server的Analysis Service服务默认使用的是2382或2383端口,但是实际上我们可以通过配置文件手动更改SSAS使用其它端口号. 修改SSAS使用端口号的方法如下,找到你的 ...

  10. 最长上升子序列O(nlogn)算法详解

    最长上升子序列 时间限制: 10 Sec   内存限制:128 MB 题目描述 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.我们想知道此时最长上升子 ...