01背包模板-HDU 2602 Bone Collector-dp练习赛
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 ?
InputThe 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.OutputOne 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
1 #include<iostream> 2 #define ll long long 3 using namespace std; 4 ll f[1005]; 5 ll V[1005],W[1005]; 6 ll n,t; 7 int max(int a,int b){ 8 if(a>b) return a; 9 else return b; 10 } 11 int main(){ 12 int T;cin>>T; 13 while(T--){ 14 memset(V,0,sizeof(V)); 15 memset(W,0,sizeof(W)); 16 memset(f,0,sizeof(f)); 17 cin>>n>>t; 18 for(ll i=1;i<=n;i++){ 19 cin>>V[i]; 20 } 21 for(ll i=1;i<=n;i++){ 22 cin>>W[i]; 23 } 24 for(ll i=1;i<=n;i++){ 25 for(ll j=t;j>=0;j--){ 26 if(j-W[i]>=0) 27 f[j]=max(f[j],f[j-W[i]]+V[i]); 28 else f[j]=f[j]; 29 } 30 } 31 cout<<f[t]<<endl; 32 } 33 return 0; 34 }

浙公网安备 33010602011771号