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. Lintcode: Recover Rotated Sorted Array

    Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, ...

  2. Codeforce Round #222 Div2

    这场断网,本来有个别人的比较卡的无线 但后面睡着了- -! C:额,逆向想下! B:... A:...

  3. acm pc^2的配置与使用

    -------------------------------------------------------------------------------------- !!! 转载请注明: 转自 ...

  4. [摘录]quarts:Quartz Quick Start Guide

    (Primarily authored by Dafydd James) Welcome to the QuickStart guide for Quartz. As you read this gu ...

  5. vs2003打包

    怎样将.Net程序部署到没有安装.Net Framwork的机器上? 部署在.Net 平台下开发的应用程序,需要安装安装对应版本的.Net Framwork,而Vsual Studio 2003并没有 ...

  6. c++之路进阶——hdu3507(Print Article)

    参考博文:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html//讲的真的很好,有个小错误,博客里的num全为sum,像我这种 ...

  7. HDU 4513 吉哥系列故事——完美队形II(Manacher)

    Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成 ...

  8. (七)DAC0832 数模转换芯片的应用 以及运算放大器的学习 01

    DAC0832是8分辨率的D/A转换集成芯片.与微处理器完全兼容.这个DA芯片以其价格低廉.接口简单.转换控制容易等优点,在单片机应用系统中得到广泛的应用.D/A转换器由8位输入锁存器.8位DAC寄存 ...

  9. 类名.class, class.forName(), getClass()区别

    1:Class cl=A.class; JVM将使用类A的类装载器, 将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作.返回类A的Class的对象. 2:Class cl=对象引 ...

  10. ThinkPHP讲解(二)控制器

    在这一节,具体讲解控制器,以Jiaowu应用目录为例. 1.如何写控制器,如何写操作方法? 在模块控制器目录Controller下新建一个控制器文件MainController.class.php,写 ...