mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 22:56:29 +08:00
a005c6ebd3
* Update avatar's link in the landing page * Bug fixes * Move assets folder from overrides to docs * Reduce figures' corner radius * Update copyright * Update header image * Krahets -> krahets * Update the landing page
64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
/**
|
|
* File: unbounded_knapsack.cpp
|
|
* Created Time: 2023-07-11
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
#include "../utils/common.hpp"
|
|
|
|
/* 完全背包:动态规划 */
|
|
int unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
|
|
int n = wgt.size();
|
|
// 初始化 dp 表
|
|
vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));
|
|
// 状态转移
|
|
for (int i = 1; i <= n; i++) {
|
|
for (int c = 1; c <= cap; c++) {
|
|
if (wgt[i - 1] > c) {
|
|
// 若超过背包容量,则不选物品 i
|
|
dp[i][c] = dp[i - 1][c];
|
|
} else {
|
|
// 不选和选物品 i 这两种方案的较大值
|
|
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
|
|
}
|
|
}
|
|
}
|
|
return dp[n][cap];
|
|
}
|
|
|
|
/* 完全背包:空间优化后的动态规划 */
|
|
int unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
|
|
int n = wgt.size();
|
|
// 初始化 dp 表
|
|
vector<int> dp(cap + 1, 0);
|
|
// 状态转移
|
|
for (int i = 1; i <= n; i++) {
|
|
for (int c = 1; c <= cap; c++) {
|
|
if (wgt[i - 1] > c) {
|
|
// 若超过背包容量,则不选物品 i
|
|
dp[c] = dp[c];
|
|
} else {
|
|
// 不选和选物品 i 这两种方案的较大值
|
|
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
|
}
|
|
}
|
|
}
|
|
return dp[cap];
|
|
}
|
|
|
|
/* Driver code */
|
|
int main() {
|
|
vector<int> wgt = {1, 2, 3};
|
|
vector<int> val = {5, 11, 15};
|
|
int cap = 4;
|
|
|
|
// 动态规划
|
|
int res = unboundedKnapsackDP(wgt, val, cap);
|
|
cout << "不超过背包容量的最大物品价值为 " << res << endl;
|
|
|
|
// 空间优化后的动态规划
|
|
res = unboundedKnapsackDPComp(wgt, val, cap);
|
|
cout << "不超过背包容量的最大物品价值为 " << res << endl;
|
|
|
|
return 0;
|
|
}
|