-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
40 lines (40 loc) · 1.3 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
class Solution {
public int reachableNodes(int[][] edges, int maxMoves, int n) {
List<int[]>[] g = new List[n];
Arrays.setAll(g, e -> new ArrayList<>());
for (var e : edges) {
int u = e[0], v = e[1], cnt = e[2] + 1;
g[u].add(new int[] {v, cnt});
g[v].add(new int[] {u, cnt});
}
int[] dist = new int[n];
Arrays.fill(dist, 1 << 30);
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[0] - b[0]);
q.offer(new int[] {0, 0});
dist[0] = 0;
while (!q.isEmpty()) {
var p = q.poll();
int d = p[0], u = p[1];
for (var nxt : g[u]) {
int v = nxt[0], cnt = nxt[1];
if (d + cnt < dist[v]) {
dist[v] = d + cnt;
q.offer(new int[] {dist[v], v});
}
}
}
int ans = 0;
for (int d : dist) {
if (d <= maxMoves) {
++ans;
}
}
for (var e : edges) {
int u = e[0], v = e[1], cnt = e[2];
int a = Math.min(cnt, Math.max(0, maxMoves - dist[u]));
int b = Math.min(cnt, Math.max(0, maxMoves - dist[v]));
ans += Math.min(cnt, a + b);
}
return ans;
}
}