Bone CollectorTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 38586 Accepted Submission (s): 16007
Problem Descriptionmany years ago, in Teddy ' s hometown there is a man who was called "Bone Collector". Collect varies of bones, such as dog ' s, cow ' s, also he went to the grave ...
The bone collector had a big bag with a volume of V, and along he trip of collecting there is a lot of bones, obviously , different bone have different value and different volume, now given the each bone's value along his trips, can you CALCU Late out the maximum of the total value the bone collector can get?
Inputthe first line contain a integer T, the number of cases.
Followed by T cases, each case three lines, the first line contain both integer n, V, (N <=, v <=) repr Esenting the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume for each bone.
Outputone integer per line representing the maximum of the total value (this number would be is less than 231).
Sample Input
15 101 2 3 4 55 4 3 2 1
Sample Output
14
The size and value of each bone in a backpack of a given capacity V to find the maximum value of a backpack to fit and
01 knapsack problem is the one-time use of bone methods common with two-dimensional arrays and one-dimensional arrays is a two-dimensional array is optimized for the optimization of the cyclic array
The method of state transfer equation of two-dimensional array is explained first.
DP[I][V] = max (Dp[i-1][v], dp[i-1][v-c[i]]+w[i])
Placing the first I item in a backpack with a capacity of V is a sub-problem if you only consider the strategy of item I (put or not) then it can be converted into a problem that only involves the first I-1 items
The implementation code is as follows
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 1010;int va [MAXN], Vo[maxn];int dp[maxn][maxn];int N, v;int res;int main () { int t; scanf ("%d", &t); while (t--) { scanf ("%d%d", &n, &v); for (int i = 1; I <= n; ++i) { scanf ("%d", &va[i]); } for (int i = 1; I <= n; ++i) { scanf ("%d", &vo[i]); } memset (DP, 0, sizeof (DP)); for (int i = 1, i <= N; ++i) {for (int j = 0; J <= v; ++j) { if (Vo[i] <= j) Dp[i][j] = max (dp[i-1][j) , Dp[i-1][j-vo[i]]+va[i]); else dp[i][j]=dp[i-1][j]; } } printf ("%d\n", Dp[n][v]); } return 0;}
Now let's talk about the method of one-dimensional array recursive public-
For I <-1 to N
Do-v <-v to 0
Do dp[v] = max (Dp[v], dp[v-c[i]]+w[i])
This is because of the use of a scrolling array of feeling so the second cycle to reverse order to ensure that every DP update is from the previous layer of i-1 update to get
The code is implemented as follows
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 1010;int va [MAXN], Vo[maxn];int dp[maxn];int N, v;int res;int main () { int t; scanf ("%d", &t); while (t--) { scanf ("%d%d", &n, &v); for (int i = 0; i < n; ++i) { scanf ("%d", &va[i]); } for (int i = 0; i < n; ++i) { scanf ("%d", &vo[i]); } memset (DP, 0, sizeof (DP)); for (int i = 0; i < n; ++i) {for (int j = v; j >= Vo[i];--j) { dp[j] = max (Dp[j], Dp[j-vo[i]] + va[i);
} } printf ("%d\n", Dp[v]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"HDU2602" Bone Collector (01 backpack)