mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 11:46:29 +08:00
60 lines
1.8 KiB
Rust
60 lines
1.8 KiB
Rust
/*
|
|
* File: unbounded_knapsack.rs
|
|
* Created Time: 2023-07-09
|
|
* Author: sjinzh (sjinzh@gmail.com)
|
|
*/
|
|
|
|
/* 完全背包:动态规划 */
|
|
fn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {
|
|
let n = wgt.len();
|
|
// 初始化 dp 表
|
|
let mut dp = vec![vec![0; cap + 1]; n + 1];
|
|
// 状态转移
|
|
for i in 1..=n {
|
|
for c in 1..=cap {
|
|
if wgt[i - 1] > c as i32 {
|
|
// 若超过背包容量,则不选物品 i
|
|
dp[i][c] = dp[i - 1][c];
|
|
} else {
|
|
// 不选和选物品 i 这两种方案的较大值
|
|
dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);
|
|
}
|
|
}
|
|
}
|
|
return dp[n][cap];
|
|
}
|
|
|
|
/* 完全背包:空间优化后的动态规划 */
|
|
fn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {
|
|
let n = wgt.len();
|
|
// 初始化 dp 表
|
|
let mut dp = vec![0; cap + 1];
|
|
// 状态转移
|
|
for i in 1..=n {
|
|
for c in 1..=cap {
|
|
if wgt[i - 1] > c as i32 {
|
|
// 若超过背包容量,则不选物品 i
|
|
dp[c] = dp[c];
|
|
} else {
|
|
// 不选和选物品 i 这两种方案的较大值
|
|
dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);
|
|
}
|
|
}
|
|
}
|
|
dp[cap]
|
|
}
|
|
|
|
/* Driver Code */
|
|
pub fn main() {
|
|
let wgt = [ 1, 2, 3 ];
|
|
let val = [ 5, 11, 15 ];
|
|
let cap: usize = 4;
|
|
|
|
// 动态规划
|
|
let res = unbounded_knapsack_dp(&wgt, &val, cap);
|
|
println!("不超过背包容量的最大物品价值为 {res}");
|
|
|
|
// 空间优化后的动态规划
|
|
let res = unbounded_knapsack_dp_comp(&wgt, &val, cap);
|
|
println!("不超过背包容量的最大物品价值为 {res}");
|
|
}
|