Minimum Score of a Path Between Two Cities

There are n cities numbered 1 to n and a list of two-way roads, where roads[i] = [a, b, distance]. The score of a path is the smallest distance among the roads it uses. A path may repeat roads and cities, and may visit 1 and n many times. Return the minimum possible score of a path from city 1 to city n. A path between them is guaranteed to exist.

30:00

Minimum Score of a Path Between Two Cities
medium
Free written solution
Topics
Companies

There are n cities numbered 1 to n and a list of two-way roads, where roads[i] = [a, b, distance]. The score of a path is the smallest distance among the roads it uses. A path may repeat roads and cities, and may visit 1 and n many times. Return the minimum possible score of a path from city 1 to city n. A path between them is guaranteed to exist.

Example 1:
Input: {"n":4,"roads":[[1,2,9],[2,3,6],[2,4,5],[1,4,7]]}
Output: 5
Constraints:
  • 2≤n≤1052 \leq n \leq 10^5

  • 1≤roads.length≤1051 \leq \text{roads.length} \leq 10^5

  • roads[i]=[ai,bi,di]\text{roads}[i] = [a_i, b_i, d_i], 1≤ai,bi≤n1 \leq a_i, b_i \leq n, ai≠bia_i \neq b_i

  • 1≤di≤1041 \leq d_i \leq 10^4

  • No duplicate roads, and there is at least one path between 1 and n.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":4,"roads":[[1,2,9],[2,3,6],[2,4,5],[1,4,7]]}

4 cities and 4 two-way roads. A path may repeat roads, so it can wander to any road reachable from city 1 and still come back.

96571234

scanning

scanned road

cheapest so far

city 1 and n

BFS queue

empty

best road so far

∞

Variables
VariableValue
bestinf
queueempty
DepthFunction Call
Stack empty
0/18