Bone Collector
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 35815 Accepted Submission (s): 14753
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 Input15 101 2 3 4 55 4 3 2 1
Sample Output14
Authorteddy
Sourcehdu 1st "Vegetable-birds Cup" programming Open Contest
Recommendlcy | We have carefully selected several similar problems for you:1203 2159 2955 1171 2191
Main topic:
That is, give you a n and V and n items of v[i] and w[i], let you find out, and each time you put an item in the backpack, let you find out the maximum number of items (the volume of the object <=v) can bring the greatest value.
Problem Solving Ideas:
Go straight to the 0-1 backpack model:
Definition status: dp[i+1][j] The maximum value that can be brought by an item that is not more than J is selected from the previous I item.
Initial state: Dp[0][j]==0.
State transfer equation: dp[i+1][j] = Dp[i][j], when J < W[i].
DP[I+1][J] = max (dp[i][j],dp[i][j-w[i]]+v[i]); Other
1# include<cstdio>2# include<iostream>3 4 using namespacestd;5 6 Const intMAX = ++4;7 8 intDp[max][max];9 intW[max];Ten intV[max]; One A - intMainvoid) - { the intT;cin>>T; - while(t-- ) - { - intN,VV; +Cin>>n>>VV; - for(inti =0; I < n;i++ ) + { ACin>>V[i]; at } - for(inti =0; I < n;i++ ) - { -Cin>>W[i]; - } - for(inti =0; I < n;i++ ) in { - for(intj =0; J <= vv;j++ ) to { +dp[0][J] =0; - if(J <W[i]) the { *dp[i+1][J] =Dp[i][j]; $ }Panax Notoginseng Else - { thedp[i+1][J] = max (dp[i][j],dp[i][j-w[i]]+v[i]); + } A } the } +cout<<dp[n][vv]<<Endl; - } $ $ - return 0; -}
Code:
HDU 2602 (0-1 backpack)