VOOZH about

URL: https://www.geeksforgeeks.org/dsa/number-of-ways-to-reach-at-destination-in-shortest-time/

โ‡ฑ Number of Ways to Reach Destination in Shortest Time - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Number of Ways to Reach Destination in Shortest Time

Last Updated : 18 Nov, 2025

Given an undirected weighted graph containing V vertices from 0 to V-1 represented as an adjacency list adj[][], where each adj[u] contains pairs [v, t] indicating there is an edge between nodes u and v such that it takes travel time of t to reach from u to v or v to u.

Find the number of distinct paths to reach (V-1)th node from 0th node in minimum amount of time.

Examples:

Input: adj[][] = [[[1, 2], [3, 5]],
[0, 2], [2, 3], [3, 3]],
[1, 3], [3, 4]],
[0, 5], [1, 3], [2, 4]]]

๐Ÿ‘ 1

Output: 2
Explanation: The shortest path from 0 to 3 exists via 2 paths 0 -> 3 and 0 -> 1 -> 3 each having a cost of 5.

Input: adj[][] =[[[2, 3], [4, 2], [5, 7]],
[[4, 1], [5, 4]],
[[0, 3], [3, 1], [5, 5]],
[[2, 1], [5, 3]],
[[0, 2], [1, 1], [5, 5]],
[[0, 7], [1, 4], [2, 5], [3, 3], [4, 5]]]

๐Ÿ‘ 2

Output: 4
Explanation: The shortest paths from 0 to 5 are-
0 -> 5 (7)
0 -> 4 -> 5 (2+ 5 = 7)
0 -> 4 -> 1 -> 5 (2 + 1 + 4 = 7)
0 -> 2 -> 3 -> 5 (3 + 1 + 3 = 7)

[Naive Approach] By pruned DFS

We start a DFS from node 0 and explore every possible path to the destination n-1. While exploring, we track the total time taken so far. If at any point this time exceeds the current best (shortest) time, we stop exploring that path because we already know a faster route exists through some other path. If we reach the destination, we check whether this path is faster than all previous ones; if it is, we update the shortest time and reset the count of ways to 1. If it matches the current shortest time, we simply increase the count.
During DFS, we mark nodes as visited to avoid revisiting them in the same path, and unmark them on backtracking so other paths can reuse the node. In this way, we systematically try all valid paths, prune the ones that are guaranteed to be worse, and finally count how many distinct paths achieve the minimum possible time.


Output
2

Time Complexity: O(KV), where K is the average branching factor of the vertices in the graph and V is the number of vertices in the graph.
Space Complexity: O(V) where V is the number of vertices in the graph.

[Expected Approach] Using Dijkstra's Algorithm - O(V+ E log E) Time and O(V+E) Space

Since, we need to find distinct shortest paths from source to destination and the graph contains positive edge weights, we can use dijkstra's algorithm. Here, we use Dijkstraโ€™s algorithm to track both the shortest time to each node and the number of ways to reach that node in minimum time. We maintain a minTime[] array for the best time to reach each node and a paths[] array for how many shortest paths lead there. As we relax edges, if we find a strictly better time, we update both the time and the path count. If we find an equal time, we add the number of paths from the current node. By the end, paths[V-1] gives the total number of shortest paths to the destination.


Output
2
Comment
Article Tags:
Article Tags: