Minimum Score of a Path Between Two Cities - Problem

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

example_1.py โ€” Basic Path
$ Input: n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]]
โ€บ Output: 5
๐Ÿ’ก Note: The path from city 1 to 4 with minimum score is: 1 โ†’ 4 (score = 7) or 1 โ†’ 2 โ†’ 4 (score = min(9,5) = 5). However, since we can reuse roads, we can take: 1 โ†’ 2 โ†’ 4 โ†’ 2 โ†’ 4, which gives us access to edge (2,4) with weight 5, making 5 the minimum possible score.
example_2.py โ€” Indirect Path
$ Input: n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]]
โ€บ Output: 2
๐Ÿ’ก Note: Cities 1 and 4 are in the same connected component. The path could be 1 โ†’ 2 โ†’ 1 โ†’ 3 โ†’ 4, which allows us to use the edge (1,2) with weight 2, the minimum in the component.
example_3.py โ€” Direct Connection
$ Input: n = 2, roads = [[1,2,3]]
โ€บ Output: 3
๐Ÿ’ก Note: Only one road connects cities 1 and 2, so the minimum score is 3.

Visualization

Tap to expand
๐Ÿช1๐Ÿข2๐Ÿญ3๐Ÿฌ4๐Ÿช5๐Ÿ›ฃ๏ธ 8๐Ÿ›ฃ๏ธ 3๐Ÿ›ฃ๏ธ 12๐Ÿ›ฃ๏ธ 1๐Ÿ›ฃ๏ธ 6๐Ÿš› Truck Driver's StrategyConnected Network: Depots 1โ†’2โ†’3โ†’4โ†5, Road Capacities: [8, 3, 12, 1, 6]๐ŸŽฏ Optimal Score: 1 (narrowest road in the network)Since detours are free, we can always use the best road available!
Understanding the Visualization
1
Map the Network
Build a map of all connected roads from your starting depot
2
Explore Freely
Since detours are free, explore the entire connected road network
3
Find Bottleneck
Identify the narrowest road (minimum weight) in the entire network
4
Optimal Route
Any route using this narrowest road gives the best possible score
Key Takeaway
๐ŸŽฏ Key Insight: With unlimited road reuse, the problem reduces to finding the minimum edge weight in the connected component containing both start and end cities.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(V + E)

Visit each vertex and edge at most once during BFS/DFS traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(V + E)

Adjacency list storage plus BFS queue or DFS recursion stack

n
2n
โœ“ Linear Space

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
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
25.6K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen