Bone Collector
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 29954 Accepted Submission (s): 12323
Problem Description Many years ago, in Teddy ' s hometown there were 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?
Input The 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.
Output one integer per line representing the maximum of the total value (this number would be is less than 2 31).
Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1
Sample Output
14 Solution: 01 Backpack Classic problem, for I=1....N for j=sum ..... G[I].V Dp[j]=max (Dp[j],dp[j-g[i].v]+g[i].value); Code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[1003];
int n,v;
struct bone{
int value,volume;
} B[1003];
int main ()
{
int t;
scanf ("%d", &t);
while (t--) {
scanf ("%d%d", &n,&v);
for (int i=0;i<n;i++) {
scanf ("%d", &b[i].value);
}
for (int i=0;i<n;i++) {
scanf ("%d", &b[i].volume);
}
Memset (Dp,0,sizeof (DP));
for (int i=0;i<n;i++) for
(int j=v;j>=b[i].volume;j--) {
Dp[j]=max (Dp[j],dp[j-b[i].volume]+b[i]. value);
}
printf ("%d\n", Dp[v]);
}
return 0;
}