1976. Number of Ways to Arrive at Destination
Problem
roads[i] = [u, v, time] are two-way roads between intersections 0…n − 1. Return how many different routes take the shortest possible time from intersection 0 to n − 1, modulo 10⁹ + 7.
Example: 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]] → 4
Constraints: n ≤ 200; times up to 10⁹.
Dijkstra while counting shortest paths: a strictly shorter distance to v resets ways[v] = ways[u]; an equal distance adds ways[u].
Complexity: O(E log V).
class Solution:
def countPaths(self, n: int, roads: List[List[int]]) -> int:
MOD = 10 ** 9 + 7
g = defaultdict(list)
for a, b, t in roads:
g[a].append((b, t))
g[b].append((a, t))
dist, ways = [float("inf")] * n, [0] * n
dist[0], ways[0] = 0, 1
pq = [(0, 0)]
while pq:
d, u = heappop(pq)
if d > dist[u]:
continue
for v, t in g[u]:
if d + t < dist[v]:
dist[v], ways[v] = d + t, ways[u]
heappush(pq, (dist[v], v))
elif d + t == dist[v]:
ways[v] = (ways[v] + ways[u]) % MOD
return ways[n - 1]
R = [[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]]
assert Solution().countPaths(7, R) == 4
assert Solution().countPaths(2, [[1, 0, 10]]) == 1
class Solution {
public int countPaths(int n, int[][] roads) {
final int MOD = 1_000_000_007;
List<List<int[]>> g = new ArrayList<>();
for (int i = 0; i < n; i++) g.add(new ArrayList<>());
for (int[] r : roads) { g.get(r[0]).add(new int[]{r[1], r[2]}); g.get(r[1]).add(new int[]{r[0], r[2]}); }
long[] dist = new long[n];
Arrays.fill(dist, Long.MAX_VALUE);
long[] ways = new long[n];
dist[0] = 0; ways[0] = 1;
PriorityQueue<long[]> pq = new PriorityQueue<>((a, b) -> Long.compare(a[0], b[0]));
pq.add(new long[]{0, 0});
while (!pq.isEmpty()) {
long[] cur = pq.poll();
int u = (int) cur[1];
if (cur[0] > dist[u]) continue;
for (int[] e : g.get(u)) {
long nd = cur[0] + e[1];
if (nd < dist[e[0]]) { dist[e[0]] = nd; ways[e[0]] = ways[u]; pq.add(new long[]{nd, e[0]}); }
else if (nd == dist[e[0]]) ways[e[0]] = (ways[e[0]] + ways[u]) % MOD;
}
}
return (int) ways[n - 1];
}
}
type pathHeap [][2]int // (distance, node), smallest distance first
func (h pathHeap) Len() int { return len(h) }
func (h pathHeap) Less(i, j int) bool { return h[i][0] < h[j][0] }
func (h pathHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *pathHeap) Push(x any) { *h = append(*h, x.([2]int)) }
func (h *pathHeap) Pop() any {
old := *h
x := old[len(old)-1]
*h = old[:len(old)-1]
return x
}
func countPaths(n int, roads [][]int) int {
const mod = 1_000_000_007
g := make([][][2]int, n)
for _, r := range roads {
g[r[0]] = append(g[r[0]], [2]int{r[1], r[2]})
g[r[1]] = append(g[r[1]], [2]int{r[0], r[2]})
}
dist, ways := make([]int, n), make([]int, n)
for i := range dist {
dist[i] = math.MaxInt
}
dist[0], ways[0] = 0, 1
pq := &pathHeap{{0, 0}}
for pq.Len() > 0 {
cur := heap.Pop(pq).([2]int)
d, u := cur[0], cur[1]
if d > dist[u] {
continue
}
for _, e := range g[u] {
v, t := e[0], e[1]
if d+t < dist[v] { // strictly shorter: the shortest ways to v all go through u
dist[v], ways[v] = d+t, ways[u]
heap.Push(pq, [2]int{dist[v], v})
} else if d+t == dist[v] {
ways[v] = (ways[v] + ways[u]) % mod
}
}
}
return ways[n-1]
}