First of all, I would like to thank my brother Xiaoxiao for helping you.
Directly give the state transition equation:
DP [I] [v] = max (DP [I-1] [v], DP [I-1] [V-Vol [I] + val [I]);
Vol [I] is the volume of the I-th item, and Val is the value of the I-th item
The equation means that when we consider the I items, there are only two options for I items:
Put or not put, if not put the weight is the weight of the I-1 capacity is V, if put
The obtained is the weight when the capacity of the item in the I-1 is V-Vol [I] + the weight of the item in the I Val [I], comparison
The two big ones get the maximum value of putting I items into a package with a capacity of v.
The implementation can be one-dimensional or two-dimensional.
Two-dimensional implementation:
I/J |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
2 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
1 |
2 |
3 |
3 |
3 |
3 |
0 |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
First we need to know DP [I] [v] We Need To Know DP [I-1] [v] and DP [I-] [V-Vol [I], from the analysis perspective, we don't know the vol [I]. It may be any number in 0 -- V (of course it can be greater than, but it is obviously not allowed at this time ), so we need to know all DP [I-1] [0-v], so use a for () to find these values and save them)
This code is similar
For (I: 0 -- N)
For (V: 0 -- V)
If (Vol [I]> V)
DP [I] [v] = DP [I-1] [v];
Else {
DP [I] [v] = max (DP [I-1] [v], DP [I-1] [V-Vol [I] + val [I]);
}
Then the one-dimensional implementation is based on two-dimensional implementation. In the above Code, when we calculate DP [I] [v], we only need to know all the values of DP [I-1] [0-v], while DP [I-2] [0-v], DP [I-3] [0-v] And so on before for us has no use, so we only need to every one dimension, each update array on the line. One thing to note is that the V loop will be reduced from V to 0 and cannot be increased from 0 to V, because every time we update DP [V1] (I) when using DP [V1] (when i-1) Its vone write ratio when V is small, if the incremental from 0 will make later may be used to change the data, such: we have updated DP [3], and DP [3] + val [I] is used when DP [5] is updated later. in this case, DP [3] is not what we want, because the previous data has been overwritten.
Code:
For (I: 0-n)
For (V: v-0)
If (Vol [I] <= V)
DP [v] = max (DP [v], DP [V-Vol [I] [+ val [I]);
The following are my questions on HDU, pure templates:
HDU 2062:
One-dimensional:
# Include <iostream> <br/> # define v 1005 <br/> # define n 1005 <br/> using namespace STD; <br/> int main () <br/>{< br/> int DP [v]; <br/> int tcase; <br/> int N, V; <br/> int vol [N], Val [N]; <br/> int I, j; <br/> CIN> tcase; <br/> while (tcase --) {<br/> CIN> N> V; <br/> for (I = 1; I <= N; I ++) <br/> CIN> vol [I]; <br/> for (I = 1; I <= N; I ++) <br/> CIN> Val [I]; <br/> memset (DP, 0, sizeof (DP); <br/> for (I = 1; I <= N; I ++) {<br/> for (j = V; j> = 0; j --) {<br/> If (j> = Val [I]) <br/> DP [J] = max (DP [J], DP [J-Val [I] + vol [I]); <br/>}< br/> cout <DP [v] <Endl; <br/>}< br/> return 0; <br/>}
Two-dimensional;
# Include <iostream> <br/> # define v 1005 <br/> # define n 1005 <br/> using namespace STD; <br/> struct ELEM {<br/> int X; <br/> int y; <br/>}; <br/> int DP [N] [v]; <br/> ELEM arr [N]; <br/> int main () <br/>{< br/> int tcase; <br/> int N, V; <br/> int I, j; <br/> CIN> tcase; <br/> while (tcase --) {<br/> CIN> N> V; <br/> memset (DP, 0, sizeof (DP); <br/> for (I = 1; I <= N; I ++) <br/> CIN> arr [I]. x; <br/> for (I = 1; I <= N; I ++) <br/> CIN> arr [I]. y; <br/> for (I = 1; I <= N; I ++) {<br/> for (j = V; j> = 0; j --) {<br/> If (j> = arr [I]. y) <br/> DP [I] [J] = max (DP [I-1] [J], DP [I-1] [J-Arr [I]. y] + arr [I]. x); <br/> else <br/> DP [I] [J] = DP [I-1] [J]; <br/>}< br/> cout <DP [N] [v] <Endl; <br/>}< br/> return 0; <br/>}
Finally, I would like to thank my brother Xia for his guidance. No, you can't do it ~~~