-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 181.java
More file actions
50 lines (38 loc) · 1.37 KB
/
Day 181.java
File metadata and controls
50 lines (38 loc) · 1.37 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
import java.util.*;
class Solution {
public int countPaths(int V, int[][] edges) {
int mod = (int)1e9 + 7;
List<List<int[]>> adj = new ArrayList<>();
for (int i = 0; i < V; i++) adj.add(new ArrayList<>());
for (int[] e : edges) {
int u = e[0], v = e[1], t = e[2];
adj.get(u).add(new int[]{v, t});
adj.get(v).add(new int[]{u, t});
}
long[] dist = new long[V];
Arrays.fill(dist, Long.MAX_VALUE);
int[] ways = new int[V];
PriorityQueue<long[]> pq = new PriorityQueue<>((a, b) -> Long.compare(a[0], b[0]));
dist[0] = 0;
ways[0] = 1;
pq.offer(new long[]{0, 0});
while (!pq.isEmpty()) {
long[] curr = pq.poll();
long time = curr[0];
int node = (int)curr[1];
if (time > dist[node]) continue;
for (int[] nei : adj.get(node)) {
int next = nei[0];
long newTime = time + nei[1];
if (newTime < dist[next]) {
dist[next] = newTime;
ways[next] = ways[node];
pq.offer(new long[]{newTime, next});
} else if (newTime == dist[next]) {
ways[next] = (ways[next] + ways[node]) % mod;
}
}
}
return ways[V - 1];
}
}