HDU-2602 Bone Collector

http://acm.hdu.edu.cn/showproblem.php?pid=2602

01背包:用二维数组实现。

c[n][m]表示n种物品,背包容量为m的最大价值。

状态方程为:f(n,m)=max{f(n-1,m), f(n-1,m-w[n])+P(n,m)}这就是书本上写的动态规划方程.

 

             Bone Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 24513    Accepted Submission(s): 9911

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 ?

 

 

 

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).

 

 

 

Sample Input

 

1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output

 

14
 

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 int c[1010][1010];
 6 int p[2000],w[2000];
 7 int main()
 8 {
 9     int t,n,m,i,j;
10     cin>>t;
11     while(t--)
12    {
13         memset(p,0,sizeof(p));
14         memset(w,0,sizeof(w));
15        memset(c,0,sizeof(c));
16         cin>>n>>m;
17       for(i=1;i<=n;i++)
18         cin>>p[i];
19       for(i=1;i<=n;i++)
20         cin>>w[i];
21     for(i=1;i<n+1;i++)
22     for(j=0;j<m+1;j++)
23     {
24         if(w[i]<=j)
25         {
26              if(p[i]+c[i-1][j-w[i]]>c[i-1][j])
27                  c[i][j]=p[i]+c[i-1][j-w[i]];
28              else
29                  c[i][j]=c[i-1][j];
30         }else
31              c[i][j]=c[i-1][j];
32      }
33       printf("%d\n",c[n][m]);
34 
35     }
36     return 0;
37 }

 

 

 

 

 

posted @ 2014-02-25 21:10  疯狂的癫子  阅读(197)  评论(0编辑  收藏  举报