﻿HDU 2602 Bone Collector(01背包)-Ocrosoft

# Bone Collector

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

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
```

Author
Teddy

Solution

```#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ms(a) memset(a,0,sizeof(a))
#define INF INT_MAX
#define MAXN 1000+10
using namespace std;
int main()
{
int T;
cin>>T;
int n,m;
int val[MAXN],vol[MAXN],dp[MAXN][MAXN];
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
for(int i=1;i<=n;i++)
scanf("%d",&vol[i]);
for(int i=1;i<=n;i++)//每件物品
{
for(int j=0;j<=m;j++)//分别放入容量为[0,m]的背包(物品重量可以是0)
{
if(vol[i]<=j)//如果能放入
dp[i][j]=max(dp[i-1][j],dp[i-1][j-vol[i]]+val[i]);
//1.如果前一个大,就不放
//Q:为什么后面的加上了一个可能会比原来小?
//A:dp[i-1][j-v[i].vol]表示前i-1个物品,放入j-v[i].vol的背包,
//如果放不下,那么这n-1个物品就没有放进去,
//也就是说如果放入了第n个物品,背包里只有这个物品,
//就可能比不放这个物品而放前n-1个物品要小.
else//2.后一个大,放
dp[i][j]=dp[i-1][j];//如果不能放入
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}
```