-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
743_Network_Delay_Time.java
75 lines (69 loc) · 2.5 KB
/
743_Network_Delay_Time.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
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
class Solution {
/*Map<Integer, Integer> dist;
public int networkDelayTime(int[][] times, int N, int K) {
// DFS
Map<Integer, List<int[]>> graph = new HashMap();
for (int[] edge: times) {
if (!graph.containsKey(edge[0]))
graph.put(edge[0], new ArrayList<int[]>());
graph.get(edge[0]).add(new int[]{edge[2], edge[1]});
}
for (int node: graph.keySet()) {
Collections.sort(graph.get(node), (a, b) -> a[0] - b[0]);
}
dist = new HashMap();
for (int node = 1; node <= N; ++node)
dist.put(node, Integer.MAX_VALUE);
dfs(graph, K, 0);
int ans = 0;
for (int cand: dist.values()) {
if (cand == Integer.MAX_VALUE) return -1;
ans = Math.max(ans, cand);
}
return ans;
}
public void dfs(Map<Integer, List<int[]>> graph, int node, int elapsed) {
if (elapsed >= dist.get(node)) return;
dist.put(node, elapsed);
if (graph.containsKey(node))
for (int[] info: graph.get(node))
dfs(graph, info[1], elapsed + info[0]);
}*/
Map<Integer, Integer> dist;
public int networkDelayTime(int[][] times, int N, int K) {
// Dijkstra
Map<Integer, List<int[]>> graph = new HashMap();
for (int[] edge: times) {
if (!graph.containsKey(edge[0]))
graph.put(edge[0], new ArrayList<int[]>());
graph.get(edge[0]).add(new int[]{edge[1], edge[2]});
}
dist = new HashMap();
for (int node = 1; node <= N; ++node)
dist.put(node, Integer.MAX_VALUE);
dist.put(K, 0);
boolean[] seen = new boolean[N+1];
while (true) {
int candNode = -1;
int candDist = Integer.MAX_VALUE;
for (int i = 1; i <= N; ++i) {
if (!seen[i] && dist.get(i) < candDist) {
candDist = dist.get(i);
candNode = i;
}
}
if (candNode < 0) break;
seen[candNode] = true;
if (graph.containsKey(candNode))
for (int[] info: graph.get(candNode))
dist.put(info[0],
Math.min(dist.get(info[0]), dist.get(candNode) + info[1]));
}
int ans = 0;
for (int cand: dist.values()) {
if (cand == Integer.MAX_VALUE) return -1;
ans = Math.max(ans, cand);
}
return ans;
}
}