LeetCode每日一题(2477. Minimum Fuel Cost to Report to the Capital)

There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of n cities numbered from 0 to n - 1 and exactly n - 1 roads. The capital city is city 0. You are given a 2D integer array roads where roads[i] = [ai, bi] denotes that there exists a bidirectional road connecting cities ai and bi.

There is a meeting for the representatives of each city. The meeting is in the capital city.

There is a car in each city. You are given an integer seats that indicates the number of seats in each car.

A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.

Return the minimum number of liters of fuel to reach the capital city.

Example 1:

Input: roads = [[0,1],[0,2],[0,3]], seats = 5
Output: 3

Explanation:

  • Representative1 goes directly to the capital with 1 liter of fuel.

  • Representative2 goes directly to the capital with 1 liter of fuel.

  • Representative3 goes directly to the capital with 1 liter of fuel.

    It costs 3 liters of fuel at minimum.
    It can be proven that 3 is the minimum number of liters of fuel needed.

Example 2:

Input: roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
Output: 7

Explanation:

  • Representative2 goes directly to city 3 with 1 liter of fuel.

  • Representative2 and representative3 go together to city 1 with 1 liter of fuel.

  • Representative2 and representative3 go together to the capital with 1 liter of fuel.

  • Representative1 goes directly to the capital with 1 liter of fuel.

  • Representative5 goes directly to the capital with 1 liter of fuel.

  • Representative6 goes directly to city 4 with 1 liter of fuel.

  • Representative4 and representative6 go together to the capital with 1 liter of fuel.

    It costs 7 liters of fuel at minimum.
    It can be proven that 7 is the minimum number of liters of fuel needed.

Example 3:

Input: roads = [], seats = 1
Output: 0

Explanation: No representatives need to travel to the capital city.

Constraints:

  • 1 <= n <= 105
  • roads.length == n - 1
  • roads[i].length == 2
  • 0 <= ai, bi < n
  • ai != bi
  • roads represents a valid tree.
  • 1 <= seats <= 105

假设 dp(i)返回值是从所有的 node_i 的 descendent nodes 走到 node_i 的人数、车的数量、燃料消耗量, 人数是所有 node_i 的 child node, dp(child_node)所返回的人数的加和+1, 车的数量等于人数 / seats, 燃料消耗量等于 dp(child_node)所返回的燃料消耗量的加和+dp(child_node)所返回的所有车的数量的加和, 因为有多少辆车从 child node 走到当前 node 就要消耗多少单位的燃料。如果是 leaf node,返回的人数、车数和燃料消耗量分别是 1, 1, 0。

因为首都是 0, 所以 dp(0)的燃料消耗量就是最终答案



use std::collections::{HashMap, HashSet};

impl Solution {
    fn dp(paths: &HashMap<i32, Vec<i32>>, seats: i32, curr: i32, visited: &mut HashSet<i32>) -> (i64, i64, i64) {
        visited.insert(curr);
        let mut representatives = 0;
        let mut cars = 0;
        let mut fuel = 0;
        for &next in paths.get(&curr).unwrap() {
            if visited.contains(&next) {
                continue;
            }
            let (next_representatives, next_cars, next_fuel) = Solution::dp(paths, seats, next, visited);
            representatives += next_representatives;
            cars += next_cars;
            fuel += next_fuel;
        }
        fuel += cars;
        representatives += 1;
        cars = (representatives / seats as i64) + if representatives % seats as i64 == 0 { 0 } else { 1 };
        return (representatives, cars, fuel);
    }

    pub fn minimum_fuel_cost(roads: Vec<Vec<i32>>, seats: i32) -> i64 {
        if roads.is_empty() {
            return 0;
        }
        let paths = roads.into_iter().fold(HashMap::new(), |mut m, path| {
            m.entry(path[0]).or_insert(Vec::new()).push(path[1]);
            m.entry(path[1]).or_insert(Vec::new()).push(path[0]);
            m
        });
        let (_, _, fuel) = Solution::dp(&paths, seats, 0, &mut HashSet::new());
        fuel
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值