Advanced Graphs Weighted shortest paths
480. Number of Ways to Arrive at Destination
A city has n intersections, numbered from 0 to n - 1, joined by two-way roads. Each
roads[i] = [u, v, t] means a road joins intersections u and v, and driving along it takes t minutes
in either direction.
You start at intersection 0 and want to reach intersection n - 1 as quickly as possible. Return the number
of different routes that take the least possible time. The number can be huge, so return it modulo
109 + 7. If no route leads to intersection n - 1, return 0.
A route is a sequence of roads, each one starting where the last one ended. Two routes are different if they pass through a different sequence of intersections.
Example 1
Input: n = 6
roads = [[0, 1, 2],
[0, 2, 3],
[1, 3, 4],
[2, 3, 3],
[1, 4, 5],
[3, 5, 2],
[4, 5, 1],
[2, 5, 7]]
3Explanation: The least time is 8 minutes, and three routes take it: 0 → 1 → 3 → 5, 0 → 2 → 3 → 5 and 0 → 1 → 4 → 5. The road from 2 straight to 5 gives a route of 10 minutes, which is slower.
Example 2
Input: n = 3, roads = [[1, 0, 4], [2, 1, 4], [0, 2, 8]]
2Explanation: The road straight from 0 to 2 takes 8 minutes, and so does 0 → 1 → 2. That route drives both of its roads from their second intersection to their first, which is allowed.
Example 3
Input: n = 5
roads = [[0, 1, 1000000000],
[1, 2, 1],
[3, 4, 6]]
0Explanation: No road leaves the group of intersections 0, 1 and 2, so intersection 4 cannot be reached.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 1050 ≤ roads.length ≤ 105roads[i] = [u, v, t], with0 ≤ u, v ≤ n - 1,u ≠ vand1 ≤ t ≤ 109- At most one road joins any two intersections.
Clarifying questions
What you would ask an interviewer before writing any code.