题目描述
要点
1.每组物品有若干个,同一组内的物品最多只能选一个
2.总体积不超过V
3.总价值最大
分析
状态计算
用v[i][j]表示第i组的第j个物品的体积,w[i][j]表示第i组的第j个物品的价值
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, m;
int s[N];
int v[N][N], w[N][N], f[N];
int main()
{
scanf ("%d%d", &n, &m);
for (int i = 1; i <= n; i ++)
{
scanf ("%d", &s[i]);
for (int j = 1; j <= s[i]; j ++)
scanf ("%d%d", &v[i][j], &w[i][j]);
}
for (int i = 1; i <= n; i ++)
for (int j = m; j >= 0; j --) //即for (int j = m; j >= v[i][0]; j --)
for (int k = 1; k <= s[i]; k ++)
if (v[i][k] <= j)
f[j] = max (f[j], f[j - v[i][k]] + w[i][k]);
printf ("%d\n", f[m]);
return 0;
}
标签:背包,int,----,分组,物品,include
From: https://www.cnblogs.com/LHgogo/p/16747263.html