You are in a city that consists of n intersections numbered from 0 to n - 1 with bi-directional roads between some intersections. The inputs are generated such that you can reach any intersection from any other intersection and that there is at most one road between any two intersections.
You are given an integer n and a 2D integer array roads where roads[i] = [ui, vi, timei] means that there is a road between intersections ui and vi that takes timei minutes to travel. You want to know in how many ways you can travel from intersection 0 to intersection n - 1 in the shortest amount of time.
Return the number of ways you can arrive at your destination in the shortest amount of time. Since the answer may be large, return it modulo 109 + 7.
Example 1:
Input: n = 7, roads = [[0,6,7],[0,1,2],[1,2,3],[1,3,3],[6,3,3],[3,5,1],[6,5,1],[2,5,1],[0,4,5],[4,6,2]] Output: 4 Explanation: The shortest amount of time it takes to go from intersection 0 to intersection 6 is 7 minutes. The four ways to get there in 7 minutes are: - 0 ➝ 6 - 0 ➝ 4 ➝ 6 - 0 ➝ 1 ➝ 2 ➝ 5 ➝ 6 - 0 ➝ 1 ➝ 3 ➝ 5 ➝ 6
Example 2:
Input: n = 2, roads = [[1,0,10]] Output: 1 Explanation: There is only one way to go from intersection 0 to intersection 1, and it takes 10 minutes.
Constraints:
1 <= n <= 200n - 1 <= roads.length <= n * (n - 1) / 2roads[i].length == 30 <= ui, vi <= n - 11 <= timei <= 109ui != viDescription: This approach extends the classic Dijkstra's algorithm by integrating path counting. During the process of finding the shortest path using Dijkstra's algorithm, we will maintain an additional array to count the number of ways to reach each node in the graph in the shortest time.
Steps:
Considerations: Ensure counting is done modulo 109 + 7 to handle large numbers.
This Python function utilizes Dijkstra's algorithm via a min-heap (priority queue) to compute the shortest travel time. It adds functionality to track the number of shortest paths leading to each node. Each time a node is visited, if a shorter path is found, it updates; if equally short, it increments the path count using modular arithmetic.
JavaScript
Time Complexity: O((N + E) log N), where N is the number of nodes and E is the number of edges.
Space Complexity: O(N + E) for the graph representation and additional arrays.
Description: An adaptation of the Bellman-Ford algorithm is possible here due to its suitability for graphs with non-negative weights, though less efficient, still valid for counting shortest paths under simple constraints.
Steps:
Note: Though not the most optimized under standard constraints as seen with Dijkstra's, exploring this aids learning.
The Java solution employs Bellman-Ford to establish shortest paths by iteratively relaxing all edges. Additional logic manages path counting upon equal length paths. Repeated edge checks across (n-1) rounds ensure comprehension of total optimal paths.
C++
Time Complexity: O(N * E), given every edge relaxes across nodes.
Space Complexity: O(N) for maintaining distances and counts arrays.
| Approach | Complexity |
|---|---|
| Dijkstra's Algorithm with Path Counting | Time Complexity: O((N + E) log N), where N is the number of nodes and E is the number of edges. |
| Bellman-Ford Algorithm Adaptation | Time Complexity: O(N * E), given every edge relaxes across nodes. |
G-40. Number of Ways to Arrive at Destination • take U forward • 153,402 views views
Watch 9 more video solutions →Practice Number of Ways to Arrive at Destination with our built-in code editor and test cases.
Practice on FleetCode