882. 细分图中的可到达节点
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;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧