Number of ways to arrive at destination

Graphs Shortest Path Algorithms Hard
  • Fun Fact: This programming problem resembles the widespread real life applications in route optimization, which is one of the key tasks handled by GPS and mapping services like Google Maps, Uber, and Lyft, etc
  • Their algorithms not only have to calculate the fastest route from point A to point B, but also provide alternatives in case the initial route is blocked or traffic conditions change
  • A high-level solution to this problem often involves the usage of Dijkstra's algorithm or Bellman Ford's algorithm for finding the shortest path in a graph, seen regularly in logistics, supply chain management and transport industries
  • The concept of finding the number of ways to reach a destination in the shortest time can be beneficial in providing multiple optimal route recommendations

A city consists of n intersections numbered from 0 to n - 1 with bi-directional roads between some intersections. The inputs are generated such that one can reach any intersection from any other intersection and that there is at most one road between any two intersections.


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. Determine the number of ways to travel from intersection 0 to intersection n - 1 in the shortest amount of time.


Since the answer may be large, return it modulo 109 + 7.

Examples:


Input: n=7, m=10, 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 four ways to get there in 7 minutes (which is the shortest calculated time) are:

- 0 6

- 0 4 6

- 0 1 2 5 6

- 0 1 3 5 6


Input: n=6, m=8, roads= [[0,5,8],[0,2,2],[0,1,1],[1,3,3],[1,2,3],[2,5,6],[3,4,2],[4,5,2]]

Output: 3

Explanation: 

The three ways to get there in 8 minutes (which is the shortest calculated time) are:

- 0 5

- 0 2 5

- 0 1 3 4 5

Input: n = 4, m = 4, roads = [[0, 1, 10], [1, 2, 7], [2, 3, 4], [0, 3, 3]]

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

Hints

  • Use Dijkstra’s Algorithm with a priority queue (min-heap). Maintain a distance array (dist[]) initialized to infinity (∞) for all intersections except dist[0] = 0.
  • Maintain a ways array (ways[]), where ways[i] stores the number of ways to reach intersection i using the shortest path. Initialize ways[0] = 1. Process nodes in priority queue (smallest travel time first). For each neighbor v of u, If dist[v] > dist[u] + time, update dist[v] and reset ways[v] = ways[u] and If dist[v] == dist[u] + time, increment ways[v] += ways[u] (since a new shortest path to v is found).

Company Tags

Medtronic Western Digital Electronic Arts Cloudflare JPMorgan Chase ARM Roblox Riot Games HCL Technologies Goldman Sachs Zomato Walmart IBM Ernst & Young Alibaba Databricks GE Healthcare Siemens Healthineers Boston Consulting Group Epic Games Snowflake Oracle Philips Healthcare MongoDB Docker Google Microsoft Amazon Meta Apple Netflix Adobe