HDU 2602 Bone collector
题意:
已知 \(N\) 个糖果的重量和价值. 我们有一个口袋, 最多可以装 \(V\) 重量的糖果. 问口袋最多能放多少价值的糖果进去?
思路:
01背包问题
实现:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int w[N], v[N];
int f[N][N];
int main()
{
int _;
scanf("%d", &_);
while (_--)
{
int n, m;
scanf("%d%d", &n, &m);
// 初始化
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
f[i][j] = 0;
for (int i = 1; i <= n; i++)
scanf("%d", &v[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &w[i]);
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
f[i][j] = f[i - 1][j];
// 如果当前物品的体积大于当前的容量,显然不能放入
if (w[i] <= j)
f[i][j] = max(f[i][j], f[i - 1][j - w[i]] + v[i]);
}
}
printf("%d\n", f[n][m]);
}
return 0;
}
标签:HDU,2602,int,collector,糖果,Bone
From: https://www.cnblogs.com/zxr000/p/16997489.html