Skip to content

Commit

Permalink
add: 到达首都的最少油耗
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 5, 2023
1 parent e4bacbb commit e426894
Show file tree
Hide file tree
Showing 6 changed files with 95 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/minimum_fuel_cost_to_report_to_the_capital.rs) [树, 深度优先搜索, 广度优先搜索, 图]

- LeetCode 2477. 到达首都的最少油耗 <https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital>

- [T 秒后青蛙的位置](src/graphs/frog_position_after_t_seconds.rs) [树, 深度优先搜索, 广度优先搜索, 图]

- LeetCode 1377. T 秒后青蛙的位置 <https://leetcode.cn/problems/frog-position-after-t-seconds>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions src/graphs/minimum_fuel_cost_to_report_to_the_capital.rs
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
}
}
1 change: 1 addition & 0 deletions src/graphs/mod.rs
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 tests/graphs/minimum_fuel_cost_to_report_to_the_capital_test.rs
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);
}
1 change: 1 addition & 0 deletions tests/graphs/mod.rs
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;

0 comments on commit e426894

Please sign in to comment.