Bone Collector

Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to 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 his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate 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 two integer N , V, (N <= 1000 , V <= 1000 )representing 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 of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 231).
SampleInput
1
5 10
1 2 3 4 5
5 4 3 2 1
SampleOutput
14
 1 #include <iostream>
 2 #include <string.h> 
 3 using namespace std;
 4 
 5 int max(int x,int y){
 6     return x>y?x:y;
 7 }
 8 
 9 int main()
10 {
11     int T;
12     cin>>T;
13     int v[1010],w[1010],f[1010];    
14     while(T--){
15         int N,V,i;
16         cin>>N>>V;
17         memset(v,0,sizeof(v));
18         memset(w,0,sizeof(w));
19         memset(f,0,sizeof(f));
20         for(i=1;i<=N;i++)
21             cin>>w[i];
22         for(i=1;i<=N;i++)
23             cin>>v[i];
24         for(i=1;i<=N;i++)
25             for(int j=V;j>=v[i];j--){
26 
27                 f[j]=max(f[j],f[j-v[i]]+w[i]);
28 
29             }
30         cout<<f[V]<<endl;
31     }
32     return 0;
33 }
View Code

 

posted @ 2015-10-19 10:23  Wei_java  阅读(579)  评论(0编辑  收藏  举报