You're planning a road trip across n cities numbered from 1 to n. You have a detailed map represented by a 2D array roads where roads[i] = [ai, bi, distancei] indicates a bidirectional road between cities ai and bi with the given distance.
Here's the twist: the score of any path is determined by the shortest road segment you encounter along the way. Think of it as the "weakest link" in your journey - if you're driving a large truck, your path's capability is limited by the narrowest road you must use.
Your goal: Find the minimum possible score for any path between city 1 and city n.
Important rules:
- You can use the same road multiple times
- You can visit cities multiple times
- A path is guaranteed to exist between cities 1 and n
The key insight is that since you can traverse roads multiple times and visit cities repeatedly, you need to find the minimum distance road in the entire connected component containing both cities 1 and n.
Input & Output
Visualization
Time & Space Complexity
Visit each vertex and edge at most once during BFS/DFS traversal
Adjacency list storage plus BFS queue or DFS recursion stack
Constraints
- 2 โค n โค 105
- 1 โค roads.length โค 2 ร 105
- roads[i].length == 3
- 1 โค ai, bi โค n
- ai โ bi
- 1 โค distancei โค 104
- There is at least one path between cities 1 and n