【模板】01背包、完全背包
C++版本:
01背包
for (int i = 0; i < n; i++) {
for (int j = M; j >= w[i]; j--) {
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
}
完全背包
for (int i = 0; i < n; i++) {
for (int j = w[i]; j <= M; j++) {
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
}
关注我查看更多精华内容