HDU3236 01揹包增強版

Gift Hunting

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 349    Accepted Submission(s): 120

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 V 1 (RMB). Like most other coupons, you can’t get any money back if the total price is strictly smaller than V 1. Coupon 2 is almost the same, except that it’s worth V 2. Coupons should be used separately. That means you cannot combine them into a super-coupon that’s worth V 1+V 2. 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 V 1, V 2 and n (1 <= V 1 <= 500, 1 <= V 2 <= 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 V 1 = V 2 = 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

 

 

動態規劃 ,01 揹包的進化版 , 變成了 2 個揹包 , 同時還增加了必拿的物品 .

res[k][i][j][x], 在前 k 個物品當中 , x=0 , 表示在第一張購物卷爲 i , 第二張購物卷爲 j , 並且還沒有免費拿一個物品的時候能獲得的最大 happy , x=1 時表示免費拿了一個物品時的最大 happy .

則轉移方程就是 :

res[k][i][j][1]=max(res[k-1][i][j][0]+v[k],res[k-1][i-p[k]][j][1]+v[k],res[k-1][i][j-p[k]][1]+v[k])

res[k][i][j][0]=max(res[k-1][i][j][0],res[k-1][i-p[k]][j][0]+v[k],res[k-1][i][j-p[k]][0]+v[k])

當然 , 在這裏必須優先考慮拿必拿的物品 , 而且在狀態轉移的時候也要注意 ,res[k][i][j][0] 的子狀態 res[k-1][i-p[k]][j][1]+v[k], res[k-1][i][j-p[k]][1] res[k][i][j][1] 的子狀態 ,res[k-1][i-p[k]][j][0],res[k-1][i][j-p[k]][0] 都要滿足已經拿到所有必拿的物品的情況下才能納入狀態轉移的範圍 .

 

代碼如下 :


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章