forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
49 lines (49 loc) · 1.57 KB
/
Solution.java
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
class Solution {
public boolean[] findAnswer(int n, int[][] edges) {
List<int[]>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
int m = edges.length;
for (int i = 0; i < m; ++i) {
int a = edges[i][0], b = edges[i][1], w = edges[i][2];
g[a].add(new int[] {b, w, i});
g[b].add(new int[] {a, w, i});
}
int[] dist = new int[n];
final int inf = 1 << 30;
Arrays.fill(dist, inf);
dist[0] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
pq.offer(new int[] {0, 0});
while (!pq.isEmpty()) {
var p = pq.poll();
int da = p[0], a = p[1];
if (da > dist[a]) {
continue;
}
for (var e : g[a]) {
int b = e[0], w = e[1];
if (dist[b] > dist[a] + w) {
dist[b] = dist[a] + w;
pq.offer(new int[] {dist[b], b});
}
}
}
boolean[] ans = new boolean[m];
if (dist[n - 1] == inf) {
return ans;
}
Deque<Integer> q = new ArrayDeque<>();
q.offer(n - 1);
while (!q.isEmpty()) {
int a = q.poll();
for (var e : g[a]) {
int b = e[0], w = e[1], i = e[2];
if (dist[a] == dist[b] + w) {
ans[i] = true;
q.offer(b);
}
}
}
return ans;
}
}