You're navigating through a city's road network to find the optimal route from your starting point to your destination. The city consists of n intersections numbered from 0 to n-1, connected by bi-directional roads.
Given an integer n and a 2D array roads where roads[i] = [ui, vi, timei] represents a road between intersections ui and vi that takes timei minutes to travel, your goal is to determine:
- π― Find the shortest travel time from intersection
0to intersectionn-1 - π Count how many different routes achieve this shortest time
Since there might be many optimal routes, return the count modulo 109 + 7.
Note: The city is fully connected (you can reach any intersection from any other), and there's at most one direct road between any two intersections.
Input & Output
Visualization
Time & Space Complexity
Standard Dijkstra complexity: E edges, V vertices, with priority queue operations taking O(log V)
Space for adjacency list (E), distance and ways arrays (V), and priority queue (V)
Constraints
- 1 β€ n β€ 200
- n - 1 β€ roads.length β€ n Γ (n - 1) / 2
- roads[i].length == 3
- 0 β€ ui, vi β€ n - 1
- 1 β€ timei β€ 109
- ui β vi
- All pairs (ui, vi) are unique
- Graph is connected - you can reach any intersection from any other