You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads where roads[i] = [ai, bi, distancei] indicates that there is a bidirectional road between cities ai and bi with a distance equal to distancei. The cities graph is not necessarily connected.
The score of a path between two cities is defined as the minimum distance of a road in this path.
Return the minimum possible score of a path between cities 1 and n.
Note:
A path is a sequence of roads between two cities.
It is allowed for a path to contain the same road multiple times, and you can visit cities 1 and n multiple times along the path.
The test cases are generated such that there is at least one path between 1 and n.
Example 1:
Input: n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]]
Output: 5
Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 4. The score of this path is min(9,5) = 5.
It can be shown that no other path has less score.
Example 2:
Input: n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]]
Output: 2
Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 1 -> 3 -> 4. The score of this path is min(2,2,4,7) = 2.
Constraints:
2 <= n <= 105
1 <= roads.length <= 105
roads[i].length == 3
1 <= ai, bi <= n
ai != bi
1 <= distancei <= 104
There are no repeated edges.
There is at least one path between 1 and n.
Solutions
Solution 1: DFS
Thinking
Edges may be reused and \(1\) is connected to \(n\). A path's score is its lightest edge, and any \(1\)– \(n\) walk can reach every edge of that component, so the answer is the minimum weight in the component of \(1\).
DFS from \(1\), updating the answer on every edge.
According to the problem description, each edge can be traversed multiple times, and it is guaranteed that node \(1\) and node \(n\) are in the same connected component. Therefore, the problem is actually asking for the minimum edge weight in the connected component containing node \(1\).
We first build an undirected graph \(g\) from \(\textit{roads}\), then perform DFS starting from node \(1\). While traversing the connected component, we update the answer with \(\textit{ans} = \min(\textit{ans}, w)\) for each edge visited.
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\), where \(n\) and \(m\) are the number of nodes and edges, respectively.
Method 1 walks the component of city \(1\) by recursion and keeps the minimum edge weight. A chain of roads uses a call depth equal to the node count. The same visited set also works with a queue and expands the component layer by layer.
We can also use BFS to solve this problem. Enqueue node \(1\) and expand the connected component layer by layer, updating the answer with \(\textit{ans} = \min(\textit{ans}, w)\) whenever an edge is visited.
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\), where \(n\) and \(m\) are the number of nodes and edges, respectively.
Edges may be reused and \(1\) is connected to \(n\). A path's score is its lightest edge, and any \(1\)–\(n\) walk can reach every edge of that component, so the answer is the minimum weight in the component of \(1\). With \(n \le 10^5\), recursing through that component is too deep: a chain makes the call depth \(n\).
An explicit stack starts at \(1\). When a city is popped and still unvisited, every incident edge updates the minimum and unvisited neighbors are pushed.
According to the problem description, each edge can be traversed multiple times, and it is guaranteed that node \(1\) and node \(n\) are in the same connected component. Therefore, the problem is actually asking for the minimum edge weight in the connected component containing node \(1\).
We first build an undirected graph \(g\) from \(\textit{roads}\), then walk from node \(1\) with an explicit stack. When a node is popped, we update the answer with \(\textit{ans} = \min(\textit{ans}, w)\) for each of its edges and push its unvisited neighbors.
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\), where \(n\) and \(m\) are the number of nodes and edges, respectively.