leetcode - 743
网络延迟时间
迪杰斯特拉的朴素算法流程:
邻接矩阵的方法
点击查看代码
class Solution {
public int networkDelayTime(int[][] times, int n, int k) {
//初始化一些必要参数
int INF = Integer.MAX_VALUE/2;
int[][] g = new int[n][n];
for(int[] row : g){
Arrays.fill(row, INF);
}
for(int[] t : times){
int x = t[0] - 1;
int y = t[1] - 1;
int wt = t[2];
g[x][y] = wt;
}
int[] dis = new int[n];
Arrays.fill(dis, INF);
dis[k-1] = 0;
boolean[] vis = new boolean[n];
int ans = 0;
while(true){
//开始执行dijsktra算法
//1.选择当前操作数
int x = -1;
for(int i = 0; i < n; i++){
if(!vis[i] && (x ==-1 || dis[i] < dis[x])){
x = i;
}
}
//2.更新结果
//x == -1说明结束了
if(x == -1) return ans;
//如果该节点不可达,那么会出现dis[x] == INF
if(dis[x] == INF) return -1;
//正常更新dis
vis[x] = true;
ans = dis[x];
for(int y = 0; y < n; y++){
dis[y] = Math.min(dis[y], dis[x]+g[x][y]);
}
}
}
}
##node节点多,edge少,适合用邻接表
点击查看代码
class Solution {
public int networkDelayTime(int[][] times, int n, int k) {
//初始化一些必要参数
//开一个邻接表
List<int[]>[] g = new ArrayList[n];
for(int i = 0; i < n; i++){
g[i] = new ArrayList<>();
}
for(int[] t : times){
int x = t[0] - 1;
int y = t[1] - 1;
int wt = t[2];
g[x].add(new int[]{y,wt});
}
//可以增加输出查看表内内容
for(List<int[]>a : g){
for(int[] b : a){
System.out.println(b[0]+1+","+b[1]);
}
}
int INF = Integer.MAX_VALUE/2;
// int[][] g = new int[n][n];
// for(int[] row : g){
// Arrays.fill(row, INF);
// }
// for(int[] t : times){
// int x = t[0] - 1;
// int y = t[1] - 1;
// int wt = t[2];
// g[x][y] = wt;
// }
int[] dis = new int[n];
Arrays.fill(dis, INF);
dis[k-1] = 0;
// boolean[] vis = new boolean[n];
int ans = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((a,b)-> a[0]- b[0]);
pq.offer(new int[]{0, k-1});
int count = n;
while(!pq.isEmpty()){
int[] p = pq.poll();
int disP = p[0];
int x = p[1];
if(disP > dis[x]) continue;
ans = disP;
count--;
//更新距离
for(int[] e : g[x]){
int y =e[0];
int disY = e[1]+disP;
if(disY < dis[y]){
dis[y] = disY;
pq.offer(new int[]{disY, y});
}
}
}
return count != 0?-1:ans;
}
}
leetcode - 743的更多相关文章
- [LeetCode] 743. Network Delay Time 网络延迟时间
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...
- LeetCode 743. Network Delay Time
原题链接在这里:https://leetcode.com/problems/network-delay-time/ 题目: There are N network nodes, labelled 1 ...
- Java实现 LeetCode 743 网络延迟时间(Dijkstra经典例题)
743. 网络延迟时间 有 N 个网络节点,标记为 1 到 N. 给定一个列表 times,表示信号经过有向边的传递时间. times[i] = (u, v, w),其中 u 是源节点,v 是目标节点 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- Dijkstra 算法说明与实现
Dijkstra 算法说明与实现 作者:Grey 原文地址: 博客园:Dijkstra 算法说明与实现 CSDN:Dijkstra 算法说明与实现 问题描述 问题:给定出发点,出发点到所有点的距离之和 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- [LeetCode] Network Delay Time 网络延迟时间
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- Ubuntu安装宝塔服务
Linux面板7.9.4安装脚本 查看详细安装教程 使用 SSH 连接工具,如 堡塔SSH终端 连接到您的 Linux 服务器后, 挂载磁盘 ,根据系统执行相应命令开始安装(大约2分钟完成面板安装): ...
- HVV面试
linux日志管理 1. 检查系统帐号安全(1) /etc/passwd(2) /etc/shadow(3) 特权用户(uid==0)awk -F: '$3==0{print $1}' /etc/pa ...
- 【解决问题记录】https网站中请求http资源接口报错与netERRSSLPROTOCOLERROR错误的解决
在线上的CRM网站中,对接OBS做了一个专门的公共服务,公共服务使用的http协议,在页面中调用接口时出现错误:The page at 'xxx' was loaded over HTTPS, but ...
- Netty-快速入门
---------------------------------------------------- netty是什么? Netty is an asynchronous event-driven ...
- ABC224
ABC224 D 题目大意 有一个九个点的无向图棋盘,上面有八个棋子,一次操作能将一个棋子沿边移到空点上,问将每个棋子移到与它编号相同的点最少几步. 解题思路 考虑使用 BFS. 用 string 存 ...
- golang1.23版本之前 Timer Reset方法无法正确使用
golang1.23版本之前 Timer Reset方法无法正确使用 golang1.23 之前 Reset 到底有什么问题 在 golang 的 time.Reset 文档中有这么一句话,为了防止 ...
- C# as 和 is 运算符区别和用法
前言 在C#中,as 和 is 关键字都用于处理类型转换的运算符,但它们有不同的用途和行为.本文我们将详细解释这两个运算符的区别和用法. is 运算符 is 运算符用于检查对象是否是某个特定类型,或者 ...
- LeetCode刷题小白必看!如何科学地刷题,从0到1建立你的算法体系?
大家好,我是忍者算法的作者,今天想和大家聊聊如何科学地刷题.如果你是一个刚开始刷题的小白,面对LeetCode上密密麻麻的题目感到无从下手,或者刷了一段时间却发现自己进步缓慢,那么这篇文章就是为你准备 ...
- 份额大涨! 天翼云稳居中国公有云laaS市场、laaS+PaaS市场第三!
近日,国际数据公司(IDC)最新发布的<公有云市场数据跟踪,2023Q3>报告显示,在公有云整体市场增速全面收紧的背景下,中国电信天翼云市场份额大涨,中国公有云IaaS市场份额增长至12. ...
- 喜讯!天翼云斩获NLP国际顶会比赛两项荣誉
近日,NLP国际顶会ACL(The Association for Computational Linguistics)进行的国际赛事WASSA 2023(13th Workshop on Compu ...