Minimum Score of a Path Between Two Cities
Master this topic with zero to advance depth.
Minimum Score of a Path Between Two Cities
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 distancei. 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 city 1 and city n.
Insight
If cities 1 and n are connected, you can traverse any road in their connected component multiple times. Thus, the answer is simply the minimum weight road in the entire component containing city 1.
Examples
Level I: DFS (Component Traversal)
Intuition
Since we can traverse any road in a connected component multiple times, the path score is simply the minimum weight of any road in the entire component that contains city 1 (and thus city n, if reachable). Use DFS to traverse the component and track the minimum weight encountered.
Level III: Union-Find (Component Min Edge)
Intuition
Use DSU to maintain connected components and store the minimum edge weight seen so far for each component root.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.