-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo743NetworkDelayTime.java
More file actions
90 lines (84 loc) · 2.89 KB
/
No743NetworkDelayTime.java
File metadata and controls
90 lines (84 loc) · 2.89 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.wzx.leetcode;
import java.util.*;
/**
* @author wzx
* @see <a href="https://leetcode.com/problems/network-delay-time/">https://leetcode.com/problems/network-delay-time/</a>
*/
public class No743NetworkDelayTime {
/**
* Dijkstra
* <p>
* time: O((E+V)logV)
* space: O(V)
*/
public int networkDelayTime1(int[][] times, int n, int k) {
int[] dist = new int[n + 1];
Arrays.fill(dist, Integer.MAX_VALUE);
// 起点->边
Map<Integer, List<int[]>> graph = new HashMap<>();
for (int[] edge : times) {
graph.putIfAbsent(edge[0], new LinkedList<>());
graph.get(edge[0]).add(edge);
}
// 顶点的最小堆, 存放更新好最短距离却没有更新其他顶点最短距离的顶点
// (point, distance)
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>(
Comparator.comparingInt(Map.Entry::getValue));
// 添加源点
queue.add(new AbstractMap.SimpleEntry<>(k, 0));
dist[k] = 0;
dist[0] = 0;
// 标记更新好最短距离的顶点
boolean[] visit = new boolean[n + 1];
// 从源点出发, 更新到其他点的最短距离
while (!queue.isEmpty()) {
int point = queue.poll().getKey();
// 优先队列中可能包含重复元素, 防止重复
if (visit[point]) continue;
visit[point] = true;
// 访问point的所有出边
List<int[]> edges = graph.getOrDefault(point, Collections.emptyList());
for (int[] edge : edges) {
int next = edge[1], time = edge[2];
if (visit[next]) continue;
// origin -> next => min(point -> next + origin -> point)
dist[next] = Math.min(dist[next], dist[point] + time);
// 这里没有删除旧的next结点, 因为新的next总比旧的先访问, 由于visit数组的标记, 旧结点将被跳过
queue.add(new AbstractMap.SimpleEntry<>(next, dist[next]));
}
}
int res = Arrays.stream(dist).max().getAsInt();
return res == Integer.MAX_VALUE ? -1 : res;
}
/**
* Floyd
* <p>
* time: O(V^3)
* space: O(V^2)
*/
public int networkDelayTime2(int[][] times, int n, int k) {
// 邻接矩阵
int[][] dist = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) dist[i][j] = Integer.MAX_VALUE;
}
}
// 更新直接路径
for (int[] edge : times) {
dist[edge[0] - 1][edge[1] - 1] = edge[2];
}
// 用每个顶点作为中间点更新原两点间的路径
for (int v = 0; v < n; v++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// 防止溢出
if (dist[i][v] == Integer.MAX_VALUE || dist[v][j] == Integer.MAX_VALUE) continue;
dist[i][j] = Math.min(dist[i][j], dist[i][v] + dist[v][j]);
}
}
}
int res = Arrays.stream(dist[k - 1]).max().getAsInt();
return res == Integer.MAX_VALUE ? -1 : res;
}
}