2477.Minimum Fuel Cost to Report to the Capital === ###### tags: `Medium`,`BFS`,`DFS`,`Graph`,`Tree` [2477. Minimum Fuel Cost to Report to the Capital](https://leetcode.com/problems/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:** ![](https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png) ``` 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:** ![](https://assets.leetcode.com/uploads/2022/11/16/2.png) ``` 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:** ![](https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png) ``` Input: roads = [], seats = 1 Output: 0 Explanation: No representatives need to travel to the capital city. ``` **Constraints**: * 1 <= `n` <= 10^5^ * `roads.length` == `n - 1` * `roads[i].length` == 2 * 0 <= `ai`, `bi` < `n` * `ai` != `bi` * `roads` represents a valid tree. * 1 <= `seats` <= 10^5^ ### 解答 #### C++ ```cpp= class Solution { public: vector<vector<int>> graph; long long ans = 0, s; int dfs(int node = 0, int parent = -1) { int people = 1; for (auto child : graph[node]) { if (child == parent) continue; people += dfs(child, node); } if (node != 0) ans += (people - 1) / s + 1; return people; } long long minimumFuelCost(vector<vector<int>>& roads, int seats) { int n = roads.size() + 1; s = seats; graph.resize(n); for (auto& road : roads) { graph[road[0]].push_back(road[1]); graph[road[1]].push_back(road[0]); } dfs(); return ans; } }; ``` > [name=Yen-Chi Chen][time=Sun, Feb 12, 2023] ##### 思路: 1. 每個node有一個人, 計算所有人走到node 0的最低耗油成本, 可共乘節省成本 2. 所有人最終會走到0, 所以從0為root做DFS, 第i層中一個node的耗油成本, 為其與相連i+1層node的人數/每輛車空間取ceil, 故遞迴時每經過一個node加總成本, 並回傳這一個node的人數 Time: $O(n)$ Extra Space: $O(n)$ > [name=XD] [time= Feb 12, 2023] #### Python ```python= class Solution: def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int: n = len(roads) + 1 graph = [[] for _ in range(n)] ans = 0 def dfs(node = 0, parent = -1): nonlocal ans people = 1 for child in graph[node]: if child == parent: continue people += dfs(child, node) if node != 0: ans += (people - 1) // seats + 1 return people for road in roads: graph[road[0]].append(road[1]) graph[road[1]].append(road[0]) dfs() return ans ``` > [name=Yen-Chi Chen][time=Sun, Feb 12, 2023] ### Reference [回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)