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;
}
}

链式前向星+dijkstra的更多相关文章

  1. [poj3159]Candies(差分约束+链式前向星dijkstra模板)

    题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...

  2. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  3. 洛谷P3371单源最短路径Dijkstra版(链式前向星处理)

    首先讲解一下链式前向星是什么.简单的来说就是用一个数组(用结构体来表示多个量)来存一张图,每一条边的出结点的编号都指向这条边同一出结点的另一个编号(怎么这么的绕) 如下面的程序就是存链式前向星.(不用 ...

  4. 【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)

    Dijkstra+ 链式前向星+ 优先队列   Dijkstra算法 Dijkstra最短路算法,个人理解其本质就是一种广度优先搜索.先将所有点的最短距离Dis[ ]都刷新成∞(涂成黑色),然后从起点 ...

  5. Floyd && Dijkstra +邻接表 +链式前向星(真题讲解来源:城市路)

    1381:城市路(Dijkstra) 时间限制: 1000 ms         内存限制: 65536 KB提交数: 4066     通过数: 1163 [题目描述] 罗老师被邀请参加一个舞会,是 ...

  6. 模板 Dijkstra+链式前向星+堆优化(非原创)

    我们首先来看一下什么是前向星.   前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序, 并记录下以某个点为起点的所有边在数组中的起始位置和 ...

  7. HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】

    最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...

  8. 链式前向星+SPFA

    今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...

  9. hdu2647 逆拓扑,链式前向星。

    pid=2647">原文地址 题目分析 题意 老板发工资,可是要保证发的工资数满足每一个人的期望,比方A期望工资大于B,仅仅需比B多1元钱就可以.老板发的最低工资为888元.输出老板最 ...

  10. 图的存储结构:邻接矩阵(邻接表)&链式前向星

    [概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...

随机推荐

  1. 2021-1-31 group class note

    Lesson aim By the end of this lesson, Ss will be able to talk about restaurant food using verbs of p ...

  2. 使用ms17-010对win7进行渗透(445永恒之蓝)

    永恒之蓝是指2017年4月14日晚,黑客团体Shadow Brokers(影子经纪人)公布一大批网络攻击工具,其中包含"永恒之蓝"工具,"永恒之蓝"利用Wind ...

  3. popen函数和pyinstaller打包之 -w冲突

    启发文章:https://www.jb51.net/article/184731.htm 之前我也是用到了os.popen()这个函数 1.os.popen(self.excel_path)  括号里 ...

  4. Django项目创建应用(二)

    四.创建应用 一个项目里可以创建多个应用,每个应用进行一种业务处理 (1)激活当前项目的环境 D:\pythonProject2023\djangoProject>activate python ...

  5. Error occurred while proxying request localhost:端口 报错500的解决方法

    '/AuthServer/api/': { target: 'https://localhost:44319', secure:false,// 这是签名认证,http和https区分的参数设置 ch ...

  6. update_base_x.txt

    update g_temp.test_baseset field_date = '20210101'::datewhere field_int = 6

  7. const char* str和const char str[]的区别

    首先,字符串常量是存储在flash中的.假设字符串常量在flash中的地址是0x8003fb8. 第一种方式,str等价于str的内存单元的地址,str的内存单元存储着字符串常量的地址 第二种方式,s ...

  8. 阿里云初始化,epel库,docker安装的一般步骤,和java8 升级 java11 的一些bug,无法显示验证码,等

    1. 反射异常 有些反射异常,不是自己代码的错而是一些框架调用的时候,所带来的,不好处理. 用压制输出的形式,1行为压制,2行为调试模式,输出所有的报错信息.这里用java.base / java.n ...

  9. vi 快捷键/ctags

    vi 配置 syntax enableset nu set relativenumberset hlsearch set autoindentset shiftwidth=4set tabstop=4 ...

  10. git log 查看修改历史

    git log 后面可以跟文件名,表示查看对应文件的修改记录 git log --pretty=oneline --format="%h:%ad:%an:%s" -5 git lo ...