-
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
95 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,39 @@ | ||
// 到达首都的最少油耗 | ||
// https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital | ||
// INLINE ../../images/graphs/minimum_fuel_cost_to_report_to_the_capital.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn minimum_fuel_cost(roads: Vec<Vec<i32>>, seats: i32) -> i64 { | ||
let n = roads.len() + 1; | ||
let mut graph = vec![vec![]; n]; | ||
for road in &roads { | ||
let (src, dest): (usize, usize) = (road[0] as usize, road[1] as usize); | ||
graph[src].push(dest); | ||
graph[dest].push(src); | ||
} | ||
|
||
fn traverse( | ||
node: usize, | ||
parent: i32, | ||
graph: &Vec<Vec<usize>>, | ||
cost: &mut i64, | ||
seats: i32, | ||
) -> i32 { | ||
let mut total = 1; | ||
for &neighbor in &graph[node] { | ||
if neighbor as i32 != parent { | ||
let sub_total = traverse(neighbor, node as i32, graph, cost, seats); | ||
*cost += ((sub_total + seats - 1) / seats) as i64; | ||
total += sub_total; | ||
} | ||
} | ||
total | ||
} | ||
|
||
let mut cost = 0; | ||
traverse(0, -1, &graph, &mut cost, seats); | ||
cost | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,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; |
50 changes: 50 additions & 0 deletions
50
tests/graphs/minimum_fuel_cost_to_report_to_the_capital_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,50 @@ | ||
use rust_practice::graphs::minimum_fuel_cost_to_report_to_the_capital::Solution; | ||
|
||
#[test] | ||
fn minimum_fuel_cost() { | ||
// 示例 1: | ||
// 输入:roads = [[0,1],[0,2],[0,3]], seats = 5 | ||
// 输出:3 | ||
// 解释: | ||
// - 代表 1 直接到达首都,消耗 1 升汽油。 | ||
// - 代表 2 直接到达首都,消耗 1 升汽油。 | ||
// - 代表 3 直接到达首都,消耗 1 升汽油。 | ||
// 最少消耗 3 升汽油。 | ||
|
||
let roads1 = vec![vec![0, 1], vec![0, 2], vec![0, 3]]; | ||
let seats1 = 5; | ||
assert_eq!(Solution::minimum_fuel_cost(roads1, seats1), 3); | ||
|
||
// 示例 2: | ||
// 输入:roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 | ||
// 输出:7 | ||
// 解释: | ||
// - 代表 2 到达城市 3 ,消耗 1 升汽油。 | ||
// - 代表 2 和代表 3 一起到达城市 1 ,消耗 1 升汽油。 | ||
// - 代表 2 和代表 3 一起到达首都,消耗 1 升汽油。 | ||
// - 代表 1 直接到达首都,消耗 1 升汽油。 | ||
// - 代表 5 直接到达首都,消耗 1 升汽油。 | ||
// - 代表 6 到达城市 4 ,消耗 1 升汽油。 | ||
// - 代表 4 和代表 6 一起到达首都,消耗 1 升汽油。 | ||
// 最少消耗 7 升汽油。 | ||
|
||
let roads2 = vec![ | ||
vec![3, 1], | ||
vec![3, 2], | ||
vec![1, 0], | ||
vec![0, 4], | ||
vec![0, 5], | ||
vec![4, 6], | ||
]; | ||
let seats2 = 2; | ||
assert_eq!(Solution::minimum_fuel_cost(roads2, seats2), 7); | ||
|
||
// 示例 3: | ||
// 输入:roads = [], seats = 1 | ||
// 输出:0 | ||
// 解释:没有代表需要从别的城市到达首都。 | ||
|
||
let roads3 = vec![]; | ||
let seats3 = 1; | ||
assert_eq!(Solution::minimum_fuel_cost(roads3, seats3), 0); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,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; |