{"id":9905,"date":"2022-11-26T08:41:02","date_gmt":"2022-11-26T16:41:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9905"},"modified":"2022-11-26T08:41:29","modified_gmt":"2022-11-26T16:41:29","slug":"leetcode-2477-minimum-fuel-cost-to-report-to-the-capital","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2477-minimum-fuel-cost-to-report-to-the-capital\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2477.\u00a0Minimum Fuel Cost to Report to the Capital"},"content":{"rendered":"\n<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of&nbsp;<code>n<\/code>&nbsp;cities numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>&nbsp;and exactly&nbsp;<code>n - 1<\/code>&nbsp;roads. The capital city is city&nbsp;<code>0<\/code>. You are given a 2D integer array&nbsp;<code>roads<\/code>&nbsp;where&nbsp;<code>roads[i] = [a<sub>i<\/sub>, b<sub>i<\/sub>]<\/code>&nbsp;denotes that there exists a&nbsp;<strong>bidirectional road<\/strong>&nbsp;connecting cities&nbsp;<code>a<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>b<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>There is a meeting for the representatives of each city. The meeting is in the capital city.<\/p>\n\n\n\n<p>There is a car in each city. You are given an integer&nbsp;<code>seats<\/code>&nbsp;that indicates the number of seats in each car.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of liters of fuel to reach the capital city<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/09\/22\/a4c380025e3ff0c379525e96a7d63a3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> roads = [[0,1],[0,2],[0,3]], seats = 5\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> \n- Representative<sub>1<\/sub> goes directly to the capital with 1 liter of fuel.\n- Representative<sub>2<\/sub> goes directly to the capital with 1 liter of fuel.\n- Representative<sub>3<\/sub> goes directly to the capital with 1 liter of fuel.\nIt costs 3 liters of fuel at minimum. \nIt can be proven that 3 is the minimum number of liters of fuel needed.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/11\/16\/2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> \n- Representative<sub>2<\/sub> goes directly to city 3 with 1 liter of fuel.\n- Representative<sub>2<\/sub> and representative<sub>3<\/sub> go together to city 1 with 1 liter of fuel.\n- Representative<sub>2<\/sub> and representative<sub>3<\/sub> go together to the capital with 1 liter of fuel.\n- Representative<sub>1<\/sub> goes directly to the capital with 1 liter of fuel.\n- Representative<sub>5<\/sub> goes directly to the capital with 1 liter of fuel.\n- Representative<sub>6<\/sub> goes directly to city 4 with 1 liter of fuel.\n- Representative<sub>4<\/sub> and representative<sub>6<\/sub> go together to the capital with 1 liter of fuel.\nIt costs 7 liters of fuel at minimum. \nIt can be proven that 7 is the minimum number of liters of fuel needed.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/09\/27\/efcf7f7be6830b8763639cfd01b690a.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> roads = [], seats = 1\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> No representatives need to travel to the capital city.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>roads.length == n - 1<\/code><\/li><li><code>roads[i].length == 2<\/code><\/li><li><code>0 &lt;= a<sub>i<\/sub>, b<sub>i<\/sub>&nbsp;&lt; n<\/code><\/li><li><code>a<sub>i<\/sub>&nbsp;!= b<sub>i<\/sub><\/code><\/li><li><code>roads<\/code>&nbsp;represents a valid tree.<\/li><li><code>1 &lt;= seats &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy + DFS<\/strong><\/h2>\n\n\n\n<p>To reach the minimum cost, we must share cars if possible, say X reps from children nodes to an intermediate node u on the way towards capital 0.  Then they all changes cars at node u, and we need (X + 1) \/\/ seats cars\/fuel from u to 0.<\/p>\n\n\n\n<p>We use DFS to count # of reps at each node u while accumulating the total cost.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n    long long ans = 0;\n    vector<vector<int>> g(roads.size() + 1);\n    for (const vector<int>& r : roads) {\n      g[r[0]].push_back(r[1]);\n      g[r[1]].push_back(r[0]);\n    }\n    \/\/ Returns total # of children of u.\n    function<int(int, int)> dfs = [&](int u, int p, int rep = 1) {      \n      for (int v : g[u])\n        if (v != p) rep += dfs(v, u);\n      if (u) ans += (rep + seats - 1) \/ seats;\n      return rep;\n    };\n    dfs(0, -1);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of&nbsp;n&nbsp;cities numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1&nbsp;and exactly&nbsp;n &#8211; 1&nbsp;roads. The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[33,77,88,177,28],"class_list":["post-9905","post","type-post","status-publish","format-standard","hentry","category-graph","tag-dfs","tag-graph","tag-greedy","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9905"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9905\/revisions"}],"predecessor-version":[{"id":9907,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9905\/revisions\/9907"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}