https://leetcode-cn.com/problems/network-delay-time/submissions/

// n <= 100
class Solution {
int N = 105, M = 6005;
// (邻接表-链式前向星)
int[] w = new int[M]; // 边的权重
int[] edge = new int[M]; // 边指向的节点
int[] head = new int[N]; // 存储的是某个节点的链表(节点指向边的结合)的第一个节点
int[] next = new int[M]; // 表示链表中的下一条边 int[] dist = new int[N]; // 起点到i的最短路
boolean[] vis = new boolean[N]; int n, k;
int idx = 0;
int inf = 0x3f3f3f3f; void add(int a, int b, int c) {
// 链表中添加新节点
w[idx] = c;
edge[idx] = b;
next[idx] = head[a]; head[a] = idx;
idx++;
} void dijkstra() {
// 起始先将所有的点标记为「未更新」和「距离为正无穷」
Arrays.fill(dist, inf);
Arrays.fill(vis, false);
// 只有起点最短距离为 0
dist[k] = 0;
// (点编号, 到起点的距离)
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> {
return x[1] - y[1];
});
pq.offer(new int[]{k, 0});
while (!pq.isEmpty()) {
int[] poll = pq.poll();
int index = poll[0], distance = poll[1];
if (vis[index]) continue;
vis[index] = true;
// 标记该点「已更新」,并使用该点更新其他点的「最短距离」
for (int i = head[index]; i != -1; i = next[i]) { // 链表最末端都是-1
int to = edge[i];
if (dist[to] > dist[index] + w[i]) {
dist[to] = dist[index] + w[i];
pq.offer(new int[]{to, dist[to]});
}
}
}
} public int networkDelayTime(int[][] times, int n, int k) {
this.n = n;
this.k = k;
// 初始化每个节点的链表的头结点
Arrays.fill(head, -1);
// 建图
for (int[] time: times) {
int from = time[0], to = time[1], val = time[2];
add(from, to, val);
}
// 最短路
dijkstra();
// 遍历答案
int ans = 0;
for (int i = 1; i <= n; i++) {
ans = Math.max(ans, dist[i]);
}
return ans == inf? -1: ans;
}
}

进阶:https://leetcode-cn.com/problems/count-nodes-with-the-highest-score/solution/gong-shui-san-xie-jian-tu-dfs-by-ac_oier-ujfo/

// 链式前向星 求 每一个节点能到达的节点数量
class Solution {
int[] cnt;
int N = 100010, M = 100010 * 2;
int[] head = new int[N];
int[] next = new int[M];
int[] edge = new int[M]; int idx = 0;
void add(int x, int y) {
edge[idx] = y;
next[idx] = head[x];
head[x] = idx;
idx++;
} public int countHighestScoreNodes(int[] parents) {
int n = parents.length;
cnt = new int[n];
Arrays.fill(head, -1); for (int i = 0; i < n; i++) {
int from = parents[i];
int to = i;
if (from == -1) continue;
add(from, to);
} // dfs
dfs(0);
long maxVal = 0L;
int maxNum = 0;
for (int i = 0; i < n; i++) {
long tmp = 0;
if (parents[i] == -1) { // 根节点
long res = 1;
for (int j = head[i]; j != -1; j = next[j]) {
int to = edge[j];
res *= cnt[to];
}
tmp = res;
} else if (head[i] == -1) { // 叶子节点
tmp = n-1;
} else { // 非叶子节点
long res = 1;
for (int j = head[i]; j != -1; j = next[j]) {
int to = edge[j];
res *= cnt[to];
}
tmp = res * (n-cnt[i]);
}
if (tmp > maxVal) {
maxVal = tmp;
maxNum = 1;
} else if (tmp == maxVal) {
maxNum++;
}
}
return maxNum;
} // 求以u为根节点的子树的节点数量
public int dfs(int u) {
int num = 1;
// 遍历所有的边
for (int i = head[u]; i != -1; i = next[i]) {
int to = edge[i];
num += dfs(to);
}
cnt[u] = num;
return num;
}
}

最新文章

  1. 记录一次bug解决过程:规范变量名称和mybatis的使用以及代码优化
  2. Javascript 布尔操作符总结
  3. Oracle过程及函数的参数模式,In、out、in out模式
  4. dede数据库文件导入失败的可能原因是数据表前缀不同,这里的失败指的是mysql添加了数据,但后台不显示
  5. JSTL Tag学习笔记(一)之&lt;c: /&gt;
  6. Darwin Streaming server 的 Task 类
  7. Heritrix个性化设置抓取目标
  8. 【1414软工助教】团队作业7——Alpha冲刺之事后诸葛亮 得分榜
  9. linux服务器上Apache配置多域名
  10. SpringBoot学习之Json数据交互
  11. 跨站请求伪造和cookie伪造
  12. Math的方法;Date的方法;
  13. 素数筛选-hdu1262
  14. Leetcode 109
  15. sqlserver统计日志数目
  16. Vue 2.0 v-for 响应式key, index及item.id参数对v-bind:key值造成差异研究
  17. MySQL 5.6.10 跨平台GTID复制实践
  18. HDU 2063 过山车(匈牙利算法)
  19. POJ 1118
  20. HDUOJ---1213How Many Tables

热门文章

  1. lowcodeEngine设计器源码简析(创建流程,主要对象), 怎么生成一个左侧面板
  2. 004. html篇之《标签分类和嵌套规则》
  3. 设置apt安装软件时是否保留下载的deb包(apt不清理/apt下载软件包)
  4. 3DMAX安装失败怎么办?安装3DMAX失败提示错误怎么解决?
  5. VMware VirtualCenter Servere服务不能启动
  6. jmeter测试工具安装篇
  7. oss上传,阿里云上传oss,带缩略图
  8. k8s_使用k8s部署博客系统deployment(四)
  9. linux重置密码和单用户模式
  10. How to Avoid Trivial Solutions in Physics-Informed Neural Networks