HDU - 2602 - Bone Collector

先上题目:

Bone Collector

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


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
 
  这一题说白了就是一条01背包的模板,只是因为物品数目最多有1000种,所以要么使用滚动数组,要么只用一维即可。滚动数组比较好实现,所以这里我用一维数组实现,但这里需要注意的是,如果是使用一位数组实现,因为原本的状态转移方程是:
         max{dp[i-1][j],dp[i-1][j-c[i]]+v[i]}   c[i]<=j
      dp[i][j]=dp[i-1][j]                c[i]>j
         0                    i==0 || j==0
  消去一维以后因为需要用到j-c[i]的数据,所以外面的循环是从1~n,里面的循环是从C~0(n是物品的数目,C是背包的总体积),只有这样做j-c[i]的数据才不会在被使用之前被覆盖。
 
上代码:
 
 1 #include <cstdio>
 2 #include <cstring>
 3 #define max(x,y) (x > y ? x : y)
 4 #define MAX 1001
 5 using namespace std;
 6 
 7 int dp[MAX],n,s;
 8 int v[MAX],c[MAX];
 9 
10 int main()
11 {
12     int t;
13     //freopen("data.txt","r",stdin);
14     scanf("%d",&t);
15     while(t--){
16         scanf("%d %d",&n,&s);
17         for(int i=1;i<=n;i++){
18             scanf("%d",&v[i]);
19         }
20         for(int i=1;i<=n;i++){
21             scanf("%d",&c[i]);
22         }
23         memset(dp,0,sizeof(dp));
24         for(int i=1;i<=n;i++){
25             for(int j=s;j>=0;j--){
26                 if(c[i]<=j) dp[j]=max(dp[j],(dp[j-c[i]]+v[i]));
27                 //printf("%d ",dp[j]);
28             }
29             //printf("\n");
30         }
31         printf("%d\n",dp[s]);
32     }
33     return 0;
34 }
2602

 

posted @ 2014-01-17 19:15  海拉鲁的林克  阅读(171)  评论(0编辑  收藏  举报