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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 移动端开源 IM 框架 MobileIMSDK v6.0 发布!
一.更新内容简介 本次为主要版本更新(本次更新内容见文末"MobileIMSDK v6.0更新内容 "一节),强势升级,将同时支持TCP.UDP.WebSocket三种协议,精心封 ...
- Nginx-总结列表
Nginx配置详解 Nginx实现前后端分离,反向代理.负载均衡 Nginx 专为性能优化而开发,性能是其最重要的考量,实现上非常注重效率 .它支持内核 Poll 模型,能经受高负载的考验,有报告表明 ...
- jwt-shiro-springsecurity-oauth2对比
1 实现token的方式概述 在cookie\session\token辨析一文已经知道了token这个概念,里面简单说明了token的组成就是数据+签名,给出了token实现身份验证的流程,并且详细 ...
- Java API 之集合
1. 包装类 (基本类型中没有多少我们能够使用的方法和属性,为了便捷我们需要自己去写) 针对每一种基本类型提供了对应的类的形式 --- 包装类 byte short int long float ...
- 京东从 OpenStack 改用 Kubernetes 的始末
构建集群的历史 物理机器的时代(2004年-2014年) 在2014年之前,我们公司的应用程序都部署在物理机器上.在物理机器时代,为了给即将上线的应用程序分配物理机器,我们平均需要等上一周的时间.由于 ...
- 自定义Ollama安装路径
由于Ollama的exe安装软件双击安装的时候默认是在C盘,以及后续的模型数据下载也在C盘,导致会占用C盘空间,所以这里单独写了一个自定义安装Ollama安装目录的教程. Ollama官网地址:htt ...
- SQL注入之联合查询注入
SQL注入之联合查询注入 一.联合查询注入原理 联合查询注入是一种常见的SQL注入攻击手法,其核心原理是利用SQL中的UNION操作符将多个SELECT语句的结果集合并,从而返回一个统一的结果集.在使 ...
- Python无网络安装插件
无网络安装插件 1.准备外网电脑,搭建所需python插件 2.将需要导出的插件,导出列表 pip freeze > .\req.txt 3.将插件导出到目录 pip download -r . ...
- 流程控制之Scanner
Scanner对象 可以通过scanner类(java.util.Scanner)来获取用户的输入 基本语法: Scanner s = new Scanner(System.in); 通过Scanne ...
- [国家集训队] Tree2 题解
加边删边 \(LCT\),标记下放同 \(luogu\) 线段树 \(2\) 一题. 时间复杂度 \(O(n\log n)\),第一次交的时候我维护 \(sum\) 不维护 \(sz\ WA\) 完了 ...