|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +
|
| 5 | +-* 2477. Minimum Fuel Cost to Report to the Capital *- |
| 6 | +
|
| 7 | +
|
| 8 | +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. |
| 9 | +
|
| 10 | +There is a meeting for the representatives of each city. The meeting is in the capital city. |
| 11 | +
|
| 12 | +There is a car in each city. You are given an integer seats that indicates the number of seats in each car. |
| 13 | +
|
| 14 | +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. |
| 15 | +
|
| 16 | +Return the minimum number of liters of fuel to reach the capital city. |
| 17 | +
|
| 18 | + |
| 19 | +
|
| 20 | +Example 1: |
| 21 | +
|
| 22 | +
|
| 23 | +Input: roads = [[0,1],[0,2],[0,3]], seats = 5 |
| 24 | +Output: 3 |
| 25 | +Explanation: |
| 26 | +- Representative1 goes directly to the capital with 1 liter of fuel. |
| 27 | +- Representative2 goes directly to the capital with 1 liter of fuel. |
| 28 | +- Representative3 goes directly to the capital with 1 liter of fuel. |
| 29 | +It costs 3 liters of fuel at minimum. |
| 30 | +It can be proven that 3 is the minimum number of liters of fuel needed. |
| 31 | +Example 2: |
| 32 | +
|
| 33 | +
|
| 34 | +Input: roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 |
| 35 | +Output: 7 |
| 36 | +Explanation: |
| 37 | +- Representative2 goes directly to city 3 with 1 liter of fuel. |
| 38 | +- Representative2 and representative3 go together to city 1 with 1 liter of fuel. |
| 39 | +- Representative2 and representative3 go together to the capital with 1 liter of fuel. |
| 40 | +- Representative1 goes directly to the capital with 1 liter of fuel. |
| 41 | +- Representative5 goes directly to the capital with 1 liter of fuel. |
| 42 | +- Representative6 goes directly to city 4 with 1 liter of fuel. |
| 43 | +- Representative4 and representative6 go together to the capital with 1 liter of fuel. |
| 44 | +It costs 7 liters of fuel at minimum. |
| 45 | +It can be proven that 7 is the minimum number of liters of fuel needed. |
| 46 | +Example 3: |
| 47 | +
|
| 48 | +
|
| 49 | +Input: roads = [], seats = 1 |
| 50 | +Output: 0 |
| 51 | +Explanation: No representatives need to travel to the capital city. |
| 52 | + |
| 53 | +
|
| 54 | +Constraints: |
| 55 | +
|
| 56 | +1 <= n <= 105 |
| 57 | +roads.length == n - 1 |
| 58 | +roads[i].length == 2 |
| 59 | +0 <= ai, bi < n |
| 60 | +ai != bi |
| 61 | +roads represents a valid tree. |
| 62 | +1 <= seats <= 105 |
| 63 | +
|
| 64 | +
|
| 65 | +
|
| 66 | +*/ |
| 67 | + |
| 68 | +import 'dart:collection'; |
| 69 | + |
| 70 | +class A { |
| 71 | + int ans = 0; |
| 72 | + int minimumFuelCost(List<List<int>> roads, int seats) { |
| 73 | + final List<List<int>> graph = List.filled(roads.length + 1, 0) |
| 74 | + .map((e) => List<int>.empty(growable: true)) |
| 75 | + .toList(); |
| 76 | + |
| 77 | + for (int i = 0; i < graph.length; ++i) graph[i] = <int>[]; |
| 78 | + |
| 79 | + for (List<int> road in roads) { |
| 80 | + final int u = road[0]; |
| 81 | + final int v = road[1]; |
| 82 | + graph[u].add(v); |
| 83 | + graph[v].add(u); |
| 84 | + } |
| 85 | + |
| 86 | + dfs(graph, 0, -1, seats); |
| 87 | + return ans; |
| 88 | + } |
| 89 | + |
| 90 | + int dfs(List<List<int>> graph, int u, int prev, int seats) { |
| 91 | + int people = 1; |
| 92 | + for (final int v in graph[u]) { |
| 93 | + if (v == prev) continue; |
| 94 | + people += dfs(graph, v, u, seats); |
| 95 | + } |
| 96 | + if (u > 0) |
| 97 | + // # of cars needed = ceil(people / seats) |
| 98 | + ans += (people + seats - 1) ~/ seats; |
| 99 | + return people; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class B { |
| 104 | + List<List<int>> buildGraph(List<List<int>> roads, int seats) { |
| 105 | + List<List<int>> adj = List.empty().map((e) => List<int>.empty()).toList(); |
| 106 | + for (int i = 0; i < seats; i++) adj.add(<int>[]); |
| 107 | + for (List<int> road in roads) { |
| 108 | + adj[road[0]].add(road[1]); |
| 109 | + adj[road[1]].add(road[0]); |
| 110 | + } |
| 111 | + return adj; |
| 112 | + } |
| 113 | + |
| 114 | + int minimumFuelCost(List<List<int>> roads, int seats) { |
| 115 | + int size = roads.length + 1; |
| 116 | + List<List<int>> adj = buildGraph(roads, size); |
| 117 | + Queue<int> q = Queue(); |
| 118 | + |
| 119 | + //Storing total edges of a node using which we will traverse the tree, |
| 120 | + //If a non zero node have 1 edge then that node is kind of a leaf and we can take it |
| 121 | + List<int> edges = List.filled(size, 0); |
| 122 | + for (int i = 0; i < size; i++) { |
| 123 | + edges[i] = adj[i].length; |
| 124 | + if (i != 0 && edges[i] == 1) q.add(i); |
| 125 | + } |
| 126 | + |
| 127 | + int fuel = 0; |
| 128 | + //Storing total person on each node initially 1 person is at every node |
| 129 | + List<int> totalMen = List.filled(size, 1); |
| 130 | + // Arrays.fill(totalMen,1); |
| 131 | + while (!q.isEmpty) { |
| 132 | + // remove |
| 133 | + int u = q.removeLast(); |
| 134 | + //Traveling from u to v, |
| 135 | + //There will be only 1 valid node which will satisfy edges[v]>0 as u has only 1 valid edge |
| 136 | + for (int v in adj[u]) { |
| 137 | + // A visited node will have 0 edges as we are decreasing no of nodes for both src and dest, |
| 138 | + //after visiting each node |
| 139 | + if (edges[v] > 0) { |
| 140 | + int cars = totalMen[u] ~/ seats; |
| 141 | + if (totalMen[u] % seats != 0) cars++; |
| 142 | + // From node u to node v we need " petrol equal to no. of cars required |
| 143 | + fuel += cars; |
| 144 | + //All people from node u now have reached to node v, so to remember that we are |
| 145 | + //incrementing it |
| 146 | + totalMen[v] += totalMen[u]; |
| 147 | + edges[v]--; |
| 148 | + edges[u]--; |
| 149 | + //Only add those nodes which are non-root and have become leaf now i.e., |
| 150 | + //only on one way they can go(That path will eventually make it meet root node) |
| 151 | + if (v != 0 && edges[v] == 1) q.add(v); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return fuel; |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// DFS SUCKS so deep in DART |
| 161 | +class C { |
| 162 | + int fuelCost = 0; |
| 163 | + |
| 164 | + int minimumFuelCost(List<List<int>> roads, int seats) { |
| 165 | + if (roads.length == 0) { |
| 166 | + return 0; |
| 167 | + } |
| 168 | + HashMap<int, List<int>> graph = HashMap(); |
| 169 | + for (List<int> road in roads) { |
| 170 | + graph.putIfAbsent(road[0], () => <int>[]); |
| 171 | + graph[road[0]]?.add(road[1]); |
| 172 | + graph.putIfAbsent(road[1], () => <int>[]); |
| 173 | + graph[road[1]]?.add(road[0]); |
| 174 | + } |
| 175 | + this.fuelCost = 0; |
| 176 | + for (int city in graph[0] ?? {}) { |
| 177 | + numOfChildren(graph, city, 0, seats); |
| 178 | + } |
| 179 | + return this.fuelCost; |
| 180 | + } |
| 181 | + |
| 182 | + int numOfChildren( |
| 183 | + HashMap<int, List<int>> graph, int city, int from, int seats) { |
| 184 | + if (graph[city]?.length == 1) { |
| 185 | + this.fuelCost += 1; |
| 186 | + return 1; |
| 187 | + } |
| 188 | + int children = 1; |
| 189 | + for (int child in graph[city] ?? {}) { |
| 190 | + if (child == from) { |
| 191 | + continue; |
| 192 | + } |
| 193 | + children += numOfChildren(graph, child, city, seats); |
| 194 | + } |
| 195 | + this.fuelCost += (children + seats - 1) ~/ seats; |
| 196 | + return children; |
| 197 | + } |
| 198 | +} |
0 commit comments