Skip to content

Commit

Permalink
add: 最小化旅行的价格总和
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 6, 2023
1 parent e426894 commit e7ee45a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

###

- [最小化旅行的价格总和](src/graphs/minimize_the_total_price_of_the_trips.rs) [树, 深度优先搜索, 图, 数组, 动态规划]

- LeetCode 2646. 最小化旅行的价格总和 <https://leetcode.cn/problems/minimize-the-total-price-of-the-trips>

- [到达首都的最少油耗](src/graphs/minimum_fuel_cost_to_report_to_the_capital.rs) [树, 深度优先搜索, 广度优先搜索, 图]

- LeetCode 2477. 到达首都的最少油耗 <https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions src/graphs/minimize_the_total_price_of_the_trips.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 最小化旅行的价格总和
// https://leetcode.cn/problems/minimize-the-total-price-of-the-trips
// INLINE ../../images/graphs/minimize_the_total_price_of_the_trips.jpeg
// 参考官方题解,dfs + dp

use std::cmp::min;

pub struct Solution;

impl Solution {
pub fn dfs(
node: usize,
parent: usize,
end: usize,
next: &Vec<Vec<usize>>,
count: &mut Vec<i32>,
) -> bool {
if node == end {
count[node] += 1;
return true;
}
for &child in &next[node] {
if child == parent {
continue;
}
if Solution::dfs(child, node, end, next, count) {
count[node] += 1;
return true;
}
}
false
}

pub fn dp(
node: usize,
parent: usize,
next: &Vec<Vec<usize>>,
price: &Vec<i32>,
count: &Vec<i32>,
) -> (i32, i32) {
let mut res: (i32, i32) = (price[node] * count[node], price[node] * count[node] / 2);
for &child in &next[node] {
if child == parent {
continue;
}
let (x, y) = Solution::dp(child, node, next, price, count);
res.0 += min(x, y);
res.1 += x;
}
res
}

pub fn minimum_total_price(
n: i32,
edges: Vec<Vec<i32>>,
price: Vec<i32>,
trips: Vec<Vec<i32>>,
) -> i32 {
let n = n as usize;
let mut next: Vec<Vec<usize>> = vec![vec![]; n];
for edge in &edges {
next[edge[0] as usize].push(edge[1] as usize);
next[edge[1] as usize].push(edge[0] as usize);
}
let mut count: Vec<i32> = vec![0; n];
for trip in &trips {
Solution::dfs(
trip[0] as usize,
usize::MAX,
trip[1] as usize,
&next,
&mut count,
);
}
let (x, y) = Solution::dp(0, usize::MAX, &next, &price, &count);
min(x, y)
}
}
1 change: 1 addition & 0 deletions src/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod find_the_town_judge;
pub mod flower_planting_with_no_adjacent;
pub mod frog_position_after_t_seconds;
pub mod minimum_fuel_cost_to_report_to_the_capital;
pub mod minimize_the_total_price_of_the_trips;
35 changes: 35 additions & 0 deletions tests/graphs/minimize_the_total_price_of_the_trips_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use rust_practice::graphs::minimize_the_total_price_of_the_trips::Solution;

#[test]
fn minimum_total_price() {
// 示例 1:
// 输入:n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,2,10,6], trips = [[0,3],[2,1],[2,3]]
// 输出:23
// 解释:
// 上图表示将节点 2 视为根之后的树结构。第一个图表示初始树,第二个图表示选择节点 0 、2 和 3 并使其价格减半后的树。
// 第 1 次旅行,选择路径 [0,1,3] 。路径的价格总和为 1 + 2 + 3 = 6 。
// 第 2 次旅行,选择路径 [2,1] 。路径的价格总和为 2 + 5 = 7 。
// 第 3 次旅行,选择路径 [2,1,3] 。路径的价格总和为 5 + 2 + 3 = 10 。
// 所有旅行的价格总和为 6 + 7 + 10 = 23 。可以证明,23 是可以实现的最小答案。
assert_eq!(
Solution::minimum_total_price(
4,
vec![vec![0, 1], vec![1, 2], vec![1, 3]],
vec![2, 2, 10, 6],
vec![vec![0, 3], vec![2, 1], vec![2, 3]]
),
23
);

// 示例 2:
// 输入:n = 2, edges = [[0,1]], price = [2,2], trips = [[0,0]]
// 输出:1
// 解释:
// 上图表示将节点 0 视为根之后的树结构。第一个图表示初始树,第二个图表示选择节点 0 并使其价格减半后的树。
// 第 1 次旅行,选择路径 [0] 。路径的价格总和为 1 。
// 所有旅行的价格总和为 1 。可以证明,1 是可以实现的最小答案。
assert_eq!(
Solution::minimum_total_price(2, vec![vec![0, 1]], vec![2, 2], vec![vec![0, 0]]),
1
);
}
1 change: 1 addition & 0 deletions tests/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod find_the_town_judge_test;
pub mod flower_planting_with_no_adjacent_test;
pub mod frog_position_after_t_seconds_test;
pub mod minimum_fuel_cost_to_report_to_the_capital_test;
pub mod minimize_the_total_price_of_the_trips_test;

0 comments on commit e7ee45a

Please sign in to comment.