2602 Bone Collector dp-3
Problem 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 ?
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.
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).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
不知道为什么老是WA,以后再来研究吧
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 int main() 8 { 9 int n,t,V,v[1005],w[1005],ans[1005]; 10 cin>>t; 11 while(t--) 12 { 13 cin>>n>>V; 14 for(int i=0;i<n;i++) 15 cin>>v[i]; 16 for(int i=0;i<n;i++) 17 cin>>w[i]; 18 memset(ans,0,sizeof(ans)); 19 for(int i=0;i<n;i++) 20 { 21 for(int j=V;j>=v[i];j--) 22 if(ans[j]<ans[j-v[i]]+w[i]) 23 ans[j]=ans[j-v[i]]+w[i]; 24 } 25 cout<<ans[V]<<endl; 26 } 27 return 0; 28 }
参考
http://www.cnblogs.com/yangcl/archive/2011/11/07/2240377.html