Number of Restricted Paths From First to Last Node Problem
Description
LeetCode Problem 1786.
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] = [u_i, v_i, weight_i] denotes that there is an edge between nodes u_i and v_i with weight equal to weight_i.
A path from node start to node end is a sequence of nodes [z_0, z_1,z_2, ..., z_k] such that z_0= start and z_k = end and there is an edge between z_i and z_(i+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(z_i) > distanceToLastNode(z_(i+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 10^9 + 7.
Example 1:

Input: n = 5, edges = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,10]]
Output: 3
Explanation: Each circle contains the node number in black and its distanceToLastNode value in blue. The three restricted paths are:
1) 1 --> 2 --> 5
2) 1 --> 2 --> 3 --> 5
3) 1 --> 3 --> 5
Example 2:

Input: n = 7, edges = [[1,3,1],[4,1,2],[7,3,4],[2,5,3],[5,6,1],[6,7,2],[7,5,3],[2,6,4]]
Output: 1
Explanation: Each circle contains the node number in black and its distanceToLastNode value in blue. The only restricted path is 1 --> 3 --> 7.
Constraints:
1 <= n <= 2 * 10^4n - 1 <= edges.length <= 4 * 10^4edges[i].length == 31 <= u_i, v_i <= nu_i!= v_i1 <= weight_i <= 10^5- There is at most one edge between any two nodes.
- There is at least one path between any two nodes.
Solution
Approach: Dijkstra + Counting in Distance Order
Run Dijkstra from node n to get dist[v], the shortest distance from every node to n. A path is restricted when the distance to n strictly decreases at every step, so each step goes from a node to a neighbour with a smaller dist.
Let f[u] be the number of restricted paths from u to n, with f[n] = 1. Process the nodes in increasing order of dist: for each node u, add f[v] for every neighbour v with dist[v] < dist[u]. All such neighbours come earlier in the order, so their values are already final. The answer is f[1] modulo 109 + 7. (A memoised DFS from node 1 also works, but a long chain makes the recursion deep.)
Complexity: O((n + m) log n) time and O(n + m) space, where m is the number of edges.
from collections import defaultdict
from math import inf
from heapq import heappop, heappush
class Solution:
def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:
g = defaultdict(list)
for u, v, w in edges:
g[u].append((v, w))
g[v].append((u, w))
dist = [inf] * (n + 1)
dist[n] = 0
q = [(0, n)]
while q:
d, u = heappop(q)
if d > dist[u]:
continue
for v, w in g[u]:
if dist[v] > d + w:
dist[v] = d + w
heappush(q, (dist[v], v))
mod = 10**9 + 7
f = [0] * (n + 1)
f[n] = 1
for u in sorted(range(1, n + 1), key=lambda x: dist[x]):
for v, _ in g[u]:
if dist[v] < dist[u]:
f[u] = (f[u] + f[v]) % mod
return f[1]