forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution4.java
41 lines (40 loc) · 1.17 KB
/
Solution4.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
class Solution {
private static final int INF = 0x3f3f;
public int networkDelayTime(int[][] times, int n, int k) {
int[] dist = new int[n];
boolean[] vis = new boolean[n];
List<int[]>[] g = new List[n];
for (int i = 0; i < n; ++i) {
dist[i] = INF;
g[i] = new ArrayList<>();
}
for (int[] t : times) {
int u = t[0] - 1, v = t[1] - 1, w = t[2];
g[u].add(new int[] {v, w});
}
--k;
dist[k] = 0;
Deque<Integer> q = new ArrayDeque<>();
q.offer(k);
vis[k] = true;
while (!q.isEmpty()) {
int u = q.poll();
vis[u] = false;
for (int[] ne : g[u]) {
int v = ne[0], w = ne[1];
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
if (!vis[v]) {
q.offer(v);
vis[v] = true;
}
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans = Math.max(ans, dist[i]);
}
return ans == INF ? -1 : ans;
}
}