Skip to main content
Back to problems
Leetcode
Medium
Graphs
Union Find
Shortest Path
Google
Minimum Score of a Path Between Two Cities

Find the minimum edge score among all roads that can lie on any path from city 1 to city n in a connected road network.

Acceptance 0%
Problem Statement

You are given an undirected road network with nn cities labeled from $1toton$. Each road connects two cities and has a positive score.

A city path from city $1tocityto cityn$ may pass through any number of roads. Among all roads that belong to at least one such path, determine the smallest road score.

Return that minimum score.

Because the network is connected, at least one path from city $1tocityto cityn$ always exists.

Input Format

Input

  • An integer nn, the number of cities.
  • A list of roads, where each road is represented as [u,v,score][u, v, score] meaning an undirected road between cities uu and vv with the given score.

Output Format

Output

  • Return a single integer: the minimum score of any road that can appear on a path from city $1tocityto cityn$.

Constraints

Constraints

  • 2n2 \le n
  • The road network is connected.
  • Roads are undirected.
  • Each road score is positive.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 4
roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]]

Output

5

Explanation

City 1 and city 4 are in the same connected component. The roads that can be used on some path between them have scores 9, 6, 5, and 7, so the minimum is 5.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.