Number of Restricted Paths From First to Last Node - Problem

There is an undirected weighted connected graph. You are given a positive integer n which denotes that the graph has n nodes labeled from 1 to n, and an array edges where each edges[i] = [ui, vi, weighti] denotes that there is an edge between nodes ui and vi with weight equal to weighti.

A path from node start to node end is a sequence of nodes [z0, z1, z2, ..., zk] such that z0 = start and zk = end and there is an edge between zi and zi+1 where 0 ≤ i ≤ k-1.

The distance of a path is the sum of the weights on the edges of the path. Let distanceToLastNode(x) denote the shortest distance of a path between node n and node x. A restricted path is a path that also satisfies that distanceToLastNode(zi) > distanceToLastNode(zi+1) where 0 ≤ i ≤ k-1.

Return the number of restricted paths from node 1 to node n. Since that number may be too large, return it modulo 109 + 7.

Input & Output

Example 1 — Basic Path
$ Input: n = 5, edges = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,3]]
Output: 3
💡 Note: There are 3 restricted paths: 1→2→5, 1→3→5, and 1→4→5. Each path has decreasing distance to node 5.
Example 2 — Simple Chain
$ Input: n = 2, edges = [[1,2,1]]
Output: 1
💡 Note: Only one path exists: 1→2. Distance decreases from 1 to 0, so it's a valid restricted path.
Example 3 — Complex Graph
$ Input: n = 4, edges = [[1,2,1],[2,3,1],[3,4,1],[1,3,3]]
Output: 2
💡 Note: Two restricted paths exist: 1→2→3→4 and 1→3→4. Both satisfy the decreasing distance condition.

Constraints

  • 2 ≤ n ≤ 200
  • n-1 ≤ edges.length ≤ 2 * 104
  • edges[i].length == 3
  • 1 ≤ ui, vi ≤ n
  • ui ≠ vi
  • 1 ≤ weighti ≤ 106
  • There is at most one edge between any two nodes

Visualization

Tap to expand
Number of Restricted Paths From First to Last Node INPUT 1 2 3 4 5 3 3 1 2 2 1 3 Input Values: n = 5 edges = [[1,2,3],[1,3,3], [2,3,1],[1,4,2],[5,2,2], [3,5,1],[5,4,3]] Find paths: node 1 --> node 5 with decreasing dist to n ALGORITHM STEPS 1 Run Dijkstra from node n Get shortest dist to node 5 Node: 1 2 3 4 5 Dist: 3 2 1 3 0 2 Define restricted path dist[zi] > dist[zi+1] 3 Memoized DFS from 1 Count paths to node n DFS explores: 1(d=3)-->2(d=2)-->5(d=0) 1(d=3)-->3(d=1)-->5(d=0) 1(d=3)-->2(d=2)-->3-->5 4 Sum valid paths dp[1] = sum of all paths Return mod 10^9+7 FINAL RESULT 3 Restricted Paths Found: Path 1: 1 --> 2 --> 5 dist: 3 > 2 > 0 [OK] Path 2: 1 --> 3 --> 5 dist: 3 > 1 > 0 [OK] Path 3: 1 --> 2 --> 3 --> 5 dist: 3 > 2 > 1 > 0 [OK] OUTPUT 3 Total restricted paths = 3 3 mod (10^9+7) = 3 Key Insight: Use Dijkstra to compute shortest distances from node n to all nodes. Then use memoized DFS starting from node 1, only moving to neighbors with strictly smaller distance. This ensures strictly decreasing distances along path. DP[node] = sum of DP[neighbor] for valid neighbors. Time: O((V+E)logV) TutorialsPoint - Number of Restricted Paths From First to Last Node | Dijkstra + Memoized DFS
Asked in
Google 12 Amazon 8 Microsoft 6 Facebook 4
18.5K Views
Medium Frequency
~35 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen