-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/graphs/minimize_the_total_price_of_the_trips_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters