HDU 2602
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 2 31).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
题意:输入N,V。N表示有多少骨头。V表示你的背包有多大。然后输入序列表示每个骨头的价值,再输入序列表示每个骨头的体积。
要求输出 背包装骨头可以装的最大的价值是多少....
思路:动态规划
如果可以装进背包,就把符合条件且价值最大的装进去
不可以就不装.....
这里用d[i][j]表示装第i个骨头时背包里的骨头价值,i表示第几个骨头,j表示背包里的体积.....
代码如下:
1 #include <stdio.h> 2 #include <string.h> 3 int n[1005],v[1005],d[1005][1005]; 4 int max(int x,int y) 5 { 6 return x>y?x:y; 7 } 8 int main() 9 { 10 int T; 11 scanf("%d",&T); 12 while(T--) 13 { 14 int N,V; 15 scanf("%d%d",&N,&V); 16 for(int i=1;i<=N;i++) 17 scanf("%d",&n[i]); 18 for(int j=1;j<=N;j++) 19 scanf("%d",&v[j]); 20 for(int i=1;i<=N;i++) 21 { 22 for(int j=V;j>=0;j--) 23 { 24 if(j>=v[i]) 25 d[i][j]=max(d[i-1][j],d[i-1][j-v[i]]+n[i]); 26 else 27 d[i][j]=d[i-1][j]; 28 } 29 } 30 printf("%d\n",d[N][V]); 31 32 } 33 }