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 onlyN
Gifts, numbered 1N
.
Each gift has a happiness value that measures how happy your girlfriend wocould
Be, if you get this gift for her. Some of them are special-youMust
Get it for your girlfriend (note that whether a gift is special has nothing
Do with its happiness value ).
Coupon 1 can be used to buy gifts with total price not greaterV
1
(RMB). Like most other coupons, youCan't
Get any money back if
Total price is strictly smallerV
1. Coupon 2 is almost the same,
Could t that it's worthV
2. coupons shocould be used separately. That means
You cannot combine them into a super-coupon that's worthV
1 +V
2.
You have to divide the gifts you choose into two part, one uses coupon 1,
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 IntegersV
1,V
2 andN
(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
FollowingN
Lines describes a gift with 3 integers:P
,H
AndS
, WhereP
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). OtherwiseS
= 0. The last test case is followedV
1
=V
2 =N
= 0, which shocould 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
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
Dynamic Planning
, 01
Evolution of backpacks
,
Changed
2
Backpacks
,
Added required items.
.
Set
Res [k] [I] [J] [X],
Before
K
Items
,
When
X = 0
Hour
,
Indicates that
I
RMB
,
The second shopping roll is
J
RMB
,
The maximum number of items that can be obtained when an item is not obtained for free
Happy
Value
,
When
X = 1
It indicates the maximum value when an item is taken for free.
Happy
Value
.
The transfer equation is
:
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])
Of course
,
The mandatory items must be given priority here.
,
Pay attention to the status transfer.
, Res [k] [I] [J] [0]
Sub-Status
Res [k-1] [I-P [k] [J] [1] + V [K], res [k-1] [I] [J-P [k] [1]
And
Res [k] [I] [J] [1]
Sub-Status
, Res [k-1] [I-P [k] [J] [0], Res [k-1] [I] [J-P [k] [0]
All items must be included in the status transfer only when all required items are obtained.
.
The Code is as follows:
:
# Include <stdio. h> <br/> # include <string. h> <br/> # include <algorithm> <br/> Using STD: sort; <br/> # define INF-2100000000 <br/> # define maxv1 501 <br/> # define maxv2 51 <br/> # define maxn 301 <br/> struct T <BR/>{< br/> int P, v, F; <br/> bool operator <(const t a) const <br/> {<br/> return F>. f; <br/>}< br/>} gift [maxn]; <br/> int res [maxv1] [maxv2] [2]; <br/> inline int max (int A, int B, int C = 0, int D = 0) <br />{< Br/> return (A> B? A: B)> (C> D? C: d )? (A> B? A: B) :( C> D? C: d); <br/>}< br/> int main () <br/>{< br/> int V1, V2, N, I, J, K, m, TS; <br/> int CAS = 1; <br/> while (scanf ("% d", & V1, & V2, & n )) <br/> {<br/> If (V1 = 0 & v2 = 0 & n = 0) break; <br/> memset (Res, 0, sizeof (RES); <br/> TS = 0; <br/> for (I = 1; I <= N; ++ I) <br/>{< br/> scanf ("% d", & Gift [I]. p, & Gift [I]. v, & Gift [I]. f); <br/> If (gift [I]. F = 1) TS + = gift [I]. v; <br/>}< br/> sort (gift + 1, gift + n + 1); <br/> for (k = 1; k <= N; + k) <br/>{< br/> If (gift [K]. F = 0) break; <br/> for (I = V1; I> = 0; -- I) <br/>{< br/> for (j = V2; j> = 0; -- j) <br/>{< br/> if (I> = gift [K]. P & J> = gift [K]. p) <br/> {<br/> res [I] [J] [1] = max (RES [I] [J] [0] + gift [K]. v, Res [I] [J-gift [K]. p] [1] + gift [K]. v, Res [I-gift [K]. p] [J] [1] + gift [K]. v); <br/> res [I] [J] [0] = max (RES [I] [J-gift [K]. p] [0] + gift [K]. v, Res [I-gift [K]. p] [J] [0] + gift [K]. v); <br/>}< br/> else if (I> = gift [K]. p) <br/> {<br/> res [I] [J] [1] = max (RES [I] [J] [0] + gift [K]. v, Res [I-gift [K]. p] [J] [1] + gift [K]. v); <br/> res [I] [J] [0] = res [I-gift [K]. p] [J] [0] + gift [K]. v; <br/>}< br/> else if (j> = gift [K]. p) <br/> {<br/> res [I] [J] [1] = max (RES [I] [J] [0] + gift [K]. v, Res [I] [J-gift [K]. p] [1] + gift [K]. v); <br/> res [I] [J] [0] = res [I] [J-gift [K]. p] [0] + gift [K]. v; <br/>}< br/> else <br/> {<br/> res [I] [J] [1] = res [I] [J] [0] + gift [K]. v; <br/>}< br/> m = 0; <br/> If (RES [V1] [V2] [1] <ts & res [V1] [V2] [0] <TS) M =-1; <br/> If (M = 0) <br/> {<br/> for (; k <= N; ++ K) <br/> {<br/> for (I = V1; I> = 0; -- I) <br/> {<br/> for (j = V2; j> = 0; -- j) <br/> {<br/> If (RES [I] [J] [0]> = TS) res [I] [J] [1] = max (RES [I] [J] [1], Res [I] [J] [0] + gift [K]. v); <br/> If (j> = gift [K]. P & res [I] [J-gift [K]. p] [0]> = TS) RES [I] [J] [0] = max (RES [I] [J-gift [K]. p] [0] + gift [K]. v, Res [I] [J] [0]); <br/> if (I> = gift [K]. P & res [I-gift [K]. p] [J] [0]> = TS) RES [I] [J] [0] = max (RES [I-gift [K]. p] [J] [0] + gift [K]. v, Res [I] [J] [0]); <br/> If (j> = gift [K]. P & res [I] [J-gift [K]. p] [1]> = TS) RES [I] [J] [1] = max (RES [I] [J-gift [K]. p] [1] + gift [K]. v, Res [I] [J] [1]); <br/> if (I> = gift [K]. P & res [I-gift [K]. p] [J] [1]> = TS) RES [I] [J] [1] = max (RES [I-gift [K]. p] [J] [1] + gift [K]. v, Res [I] [J] [1]); <br/>}< br/> for (I = 1; I <= V1; ++ I) <br/>{< br/> for (j = 1; j <= V2; ++ J) <br/> {<br/> M = max (M, Res [I] [J] [0], Res [I] [J] [1]); <br/>}< br/> printf ("case % d: % d/n", CAS ++, m); <br/>}< br/> return 0; <br/>}</P> <p>