AcWing 9. 分组背包问题 AC_any
原题链接
中等
作者:
AC_any
,
2021-04-09 22:59:59
,
所有人可见
,
阅读 264
细节注意
- 不考虑组内细节,是个01背包
- 考虑组内细节,需要逐个询问
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, m;
int v[N][N], w[N][N], s[N];
int f[N];
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
{
cin >> s[i];
for (int j = 0; j < s[i]; j ++ )
cin >> v[i][j] >> w[i][j];
}
for (int i = 1; i <= n; i ++ )
for (int j = m; j >= 0; j -- )//目前还是01背包
for (int k = 0; k < s[i]; k ++ )//询问组内的每个元素
if(j - v[i][k]>=0)
f[j] = max(f[j], f[j - v[i][k]] + w[i][k]);
cout << f[m] << endl;
return 0;
}