hdoj2602 Bone Collector(DP,01背包)

题目链接

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

题意

有n块骨头,每块骨头体积为volume,价值为value,还有一个容量为v的背包,现在将骨头装入背包中,求所装入骨头价值的最大值。

思路

01背包问题,使用动态规划解决。

代码

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6 
 7 const int N = 1010;
 8 int value[N], volume[N];
 9 int dp[N];
10 
11 int main()
12 {
13     //freopen("hdoj2602.txt", "r", stdin);
14     int t;
15     cin >> t;
16     while (t--)
17     {
18         memset(dp, 0, sizeof(dp));
19         memset(value, 0, sizeof(value));
20         memset(volume, 0, sizeof(volume));
21 
22         int n, v;
23         cin >> n >> v;
24         for (int i = 1;i <= n; i++)
25             cin >> value[i];
26         for (int i = 1; i <= n; i++)
27             cin >> volume[i];
28 
29         for (int i = 1; i <= n; i++)
30         {
31             for (int j = v; j >= volume[i]; j--)
32                 dp[j] = max(dp[j], dp[j - volume[i]] + value[i]);
33         }
34         cout << dp[v] << endl;
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/sench/p/8011948.html