HDU

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602

********************************************

分析:背包模型

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <stack>
 7 #include <map>
 8 #include <vector>
 9 using namespace std;
10 
11 #define N 2500
12 #define INF 0x3f3f3f3f
13 
14 int p[N],v[N],dp[25000];
15 
16 int main()
17 {
18     int T,n,m,i,j;
19 
20     scanf("%d", &T);
21 
22     while(T--)
23     {
24         memset(dp,0,sizeof(dp));
25 
26         scanf("%d %d",&n, &m);
27 
28         for(i=1;i<=n;i++)
29             scanf("%d", &p[i]);
30         for(i=1;i<=n;i++)
31             scanf("%d", &v[i]);
32 
33         for(i=1;i<=n;i++)
34             for(j=m;j>=v[i];j--)
35             dp[j]=max(dp[j],dp[j-v[i]]+p[i]);
36 
37         printf("%d
", dp[m]);
38     }
39     return 0;
40 }
原文地址:https://www.cnblogs.com/weiyuan/p/5775306.html