class Solution {
int N = 3010, M = 20010, INF = 0x3f3f3f3f;
int[] h = new int[N];
int[] e = new int[M];
int[] w = new int[M];
int[] ne = new int[M];
int idx;
int[] dist = new int[N];
int[] q = new int[N];
boolean[] st = new boolean[N];
void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
void spfa() {
Arrays.fill(dist, INF);
dist[0] = 0;
int hh = 0, tt = 1;
q[0] = 0;
while (hh != tt) {
int t = q[hh++];
if (hh == N) hh = 0;
st[t] = false;
for (int i = h[t]; ~i != 0; i = ne[i]) {
int j = e[i];
if (dist[j] > dist[t] + w[i]) {
dist[j] = dist[t] + w[i];
if (!st[j]) {
q[tt++] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
}
public int reachableNodes(int[][] edges, int maxMoves, int n) {
Arrays.fill(h, -1);
idx = 0;
for (int[] edge : edges) {
int a = edge[0], b = edge[1], c = edge[2];
add(a, b, c + 1);
add(b, a, c + 1);
}
spfa();
int res = 0;
for (int i = 0; i < n; i++) {
if (dist[i] <=maxMoves) {
res ++;
}
}
for (int[] edge : edges) {
int a = edge[0], b = edge[1], c = edge[2];
int x = Math.max(0, maxMoves - dist[a]), y = Math.max(0, maxMoves -dist[b]);
res += Math.min(x + y, c);
}
return res;
}
}
标签:882,dist,idx,int,tt,hh,图中,new,节点
From: https://www.cnblogs.com/eiffelzero/p/16927595.html