Problem Description
We are given n cities labeled from 1 to n and a list of bidirectional roads, where each road connects two cities with a certain distance. A path’s score is defined by the minimum distance among all roads used in that path. The objective is to find the minimum possible score along any path between city 1 and city n.
Key Insights
- The score of a path is determined by the smallest edge (distance) along that path.
- Instead of finding all paths from 1 to n, we recognize that the answer is the smallest road in the connected component reachable from city 1.
- Traversing the connected component using DFS or BFS and keeping track of the minimum encountered distance is sufficient.
- Union-Find is an alternative approach but graph traversal is more straightforward.
Space and Time Complexity
Time Complexity: O(n + m) where m is the number of roads, since every city and road is processed once. Space Complexity: O(n + m) due to the adjacency list representation and auxiliary data structures used for traversal.
Solution
We build an adjacency list for the graph and use a breadth-first search (BFS) starting from city 1 to explore the connected component that includes city 1. As we traverse each road, we continuously update the minimum distance found. This minimum value is the answer, because any path from 1 to n must lie within the connected component and will include at least one of these roads.