Bone Collector
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 39259????Accepted Submission(s): 16261
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
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2602
題意:給出n件物品的重量和價值,放進一個容量為v的背包,使背包里的價值最大。
0-1背包的模板題,可以有兩種解法。
一維數組:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int w[1100],p[1110]; int f[1110]; int main() {int t,n,v;scanf("%d",&t); while(t--){scanf("%d%d",&n,&v);for(int i=1;i<=n;i++)scanf("%d",&w[i]); //輸入物品重量 for(int i=1;i<=n;i++)scanf("%d",&p[i]); //輸入物品價值 memset(f,0,sizeof(f));for(int i=1;i<=n;i++) {for(int j=v;j>=w[i];j--) //這個循環保證了放進去的物品重量不會超過背包所能容納的重量 {if(f[j] < f[j-w[i]] + p[i]) // 如果當前所擁有價值 小于 加上這件物品時創造的價值就更新 f[j]= f[j-w[i]] + p[i]; // f[j] 表示背包重量為 j 時背包里的最大價值,// 所以f[ j - w[i] ] 表示放進這件物品時的狀態(因為放進該件物品后容量就減少了) }} printf("%d\n",f[v]);} return 0; }
二維數組:
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int w[1100],p[1110]; 6 int f[1110][1110]; 7 int main() 8 { 9 int t,n,v; 10 scanf("%d",&t); 11 while(t--) 12 { 13 scanf("%d%d",&n,&v); 14 for(int i=1;i<=n;i++) 15 scanf("%d",&p[i]); 16 for(int i=1;i<=n;i++) 17 scanf("%d",&w[i]); 18 memset(f,0,sizeof(f)); 19 for(int i=1;i<=n;i++) 20 { 21 for(int j=0;j<=v;j++) 22 { 23 if(w[i]<=j) // 這件物品的重量小于當前的容量,也就是說放的進背包 24 { 25 // f[i][j] 表示第 i 件物品在背包容量為 j 時的狀態, 26 //所以 f[i-1][j] 表示背包在上一次容量為 j 時候的狀態,也就是沒放這件物品的時候 27 f[i][j]=max(f[i-1][j],f[i-1][j-w[i]]+p[i]);// 比較 沒放進去之前 和放進該物品后 的價值,取最大 28 29 } 30 else f[i][j]=f[i-1][j]; // 如果不能放進該物品,則取上一次的狀態 31 } 32 } 33 printf("%d\n",f[n][v]); 34 } 35 return 0; 36 }
?
渣渣一枚,如果有什么不對的地方,還請各位大神批評指正~ ?(^_^)