Xx buys a pair of shoes, Has k brands, and then gives n pairs of shoes. Each pair has its brand, price, and value. Xx thinks that he is not inferior, and asks to buy a pair of each type of shoes. But in fact, he only has m cents and asked if he could buy a shoe that meets xx's requirements. If he could find it, he would output the greatest value to favorites.
Solution: the deformation of grouping backpacks. Each brand requires at least one. This is different from that of grouping, but the idea is the same. Status transfer can be performed from the previous Group (select 1) and from this group (more than 1 ).
State transition equation: dp [I] [j] = max (dp [I] [j], dp [I-1] [j-cost [I] [k] + val [I] [k], dp [I] [j-cost [\ I] [k] + val [I] [k]) (dp [I] [j] indicates the maximum value of capacity j when selected to group I, and cost [I] [k] indicates the cost of group k in group I. val is the same as above)
Test data:
5 10000 3
1 4 6
2 5 7
3 4 99
1 55 77
2 44 66
3 10000 2
1 2 3
1 3 4
1 5 6
5 10000 3
1 1000 3
1 1000 4
2 100000 4
3 1000 3
3 1000 4
Code:
[Cpp]
# Include <iostream>
# Include <string. h>
Using namespace std;
# Deprecision MAX 11000
Struct node
{
Int cos, val;
} Arr [11] [MAX];
Int dp [11] [MAX], len [11];
Int max (int a, int B ){
Return a> B? A: B;
}
Int main ()
{
Int I, j, k, t, n, m;
Int a, B, c, tot, ans, s;
While (scanf ("% d", & n, & m, & k )! = EOF ){
Memset (len, 0, sizeof (len ));
Memset (dp,-1, sizeof (dp ));
For (I = 1; I <= n; ++ I ){
Scanf ("% d", & a, & B, & c );
Len [a] ++;
Arr [a] [len [a]. cos = B;
Arr [a] [len [a]. val = c;
}
Tot = dp [0] [0] = 0;
For (I = 1; I <= k; ++ I ){
If (! Len [I]) break;
Tot ++;
For (j = 1; j <= len [I]; ++ j)
For (s = m; s> = arr [I] [j]. cos; -- s ){
If (dp [tot] [s-arr [I] [j]. cos]! =-1 ){
Int tp = dp [tot] [s-arr [I] [j]. cos] + arr [I] [j]. val;
Dp [tot] [s] = max (dp [tot] [s], tp );
}
If (dp [tot-1] [s-arr [I] [j]. cos]! =-1 ){
Int tp = dp [tot-1] [s-arr [I] [j]. cos] + arr [I] [j]. val;
Dp [tot] [s] = max (dp [tot] [s], tp );
}
} // For s
} // For I
For (ans =-1, I = m; I> = 0; -- I)
If (ans <dp [tot] [I]) ans = dp [tot] [I];
If (tot = k & ans! =-1) cout <ans <endl;
Else cout <"Impossible" <endl;
}
}
From ZeroClock