-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfWaysToDestination.java
More file actions
55 lines (42 loc) · 1.54 KB
/
NumberOfWaysToDestination.java
File metadata and controls
55 lines (42 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package graph;
import java.util.*;
public class NumberOfWaysToDestination {
public int countPaths(int n, int[][] roads) {
List<List<int[]>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
for (int[] road : roads) {
int u = road[0], v = road[1], time = road[2];
graph.get(u).add(new int[]{v, time});
graph.get(v).add(new int[]{u, time});
}
long[] dist = new long[n];
int[] ways = new int[n];
Arrays.fill(dist, Long.MAX_VALUE);
dist[0] = 0;
ways[0] = 1;
PriorityQueue<long[]> pq = new PriorityQueue<>(Comparator.comparingLong(a -> a[0]));
pq.offer(new long[]{0, 0});
int MOD = 1_000_000_007;
while (!pq.isEmpty()) {
long[] curr = pq.poll();
long d = curr[0];
int node = (int) curr[1];
if (d > dist[node])
continue;
for (int[] neighbor : graph.get(node)) {
int nextNode = neighbor[0];
int time = neighbor[1];
if (dist[node] + time < dist[nextNode]) {
dist[nextNode] = dist[node] + time;
ways[nextNode] = ways[node];
pq.offer(new long[]{dist[nextNode], nextNode});
} else if (dist[node] + time == dist[nextNode]) {
ways[nextNode] = (ways[nextNode] + ways[node]) % MOD;
}
}
}
return ways[n - 1];
}
}