Java实现 LeetCode 743 网络延迟时间(Dijkstra经典例题)
743. 网络延迟时间
有 N 个网络节点,标记为 1 到 N。
给定一个列表 times,表示信号经过有向边的传递时间。 times[i] = (u, v, w),其中 u 是源节点,v 是目标节点, w 是一个信号从源节点传递到目标节点的时间。
现在,我们从某个节点 K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 -1。
示例:

输入:times = [[2,1,1],[2,3,1],[3,4,1]], N = 4, K = 2
输出:2
注意:
N 的范围在 [1, 100] 之间。
K 的范围在 [1, N] 之间。
times 的长度在 [1, 6000] 之间。
所有的边 times[i] = (u, v, w) 都有 1 <= u, v <= N 且 0 <= w <= 100。
class Solution {
public static int maxValue=100000;
public int networkDelayTime(int[][] times, int N, int K) {
//构建邻接表,用于存放各个点到各个点的距离
int[][] matrix=new int[N+1][N+1];
for(int i=0;i<=N;i++){
for(int j=0;j<=N;j++){
matrix[i][j]=maxValue;
}
}
//遍历times填充邻接表
for(int[] time:times)
matrix[time[0]][time[1]]=time[2];
//存放 K 到各个点的最短路径,最大的那个最短路径即为结果
int[] distance = new int[N + 1];
//Arrays.fill(distance, -1);
distance[K]=0;
//判断是否找到K到达该点最短路径
boolean[] visited = new boolean[N + 1];
visited[K] = true;
for(int i=1;i<=N-1;i++){
int min=Integer.MAX_VALUE;
int index=-1;
for(int j=1;j<=N;j++){
if(!visited[j] && matrix[K][j]<min){
min=matrix[K][j];
index=j;
}
}
distance[index]=min;
visited[index]=true;
for(int k=1;k<=N;k++){
if(!visited[k] && matrix[K][index]+matrix[index][k]<matrix[K][k]){
matrix[K][k]=matrix[K][index]+matrix[index][k];
}
}
}
int maxDistance = 0;
// 遍历最大值,如果有节点未被访问,返回 -1,否则返回最大最短路径
for (int i = 1; i <= N; i++) {
if (distance[i] ==maxValue) {
return -1;
}
maxDistance = Math.max(distance[i], maxDistance);
}
return maxDistance;
}
}
Java实现 LeetCode 743 网络延迟时间(Dijkstra经典例题)的更多相关文章
- Java之线程通信的应用:经典例题:生产者/消费者问题
/** * 线程通信的应用:经典例题:生产者/消费者问题 * * 生产者(Productor)将产品交给店员(Clerk),而消费者(Customer)从店员处取走产品, * 店员一次只能持有固定数量 ...
- Java攻城狮之基础练习题------经典例题
(一)键盘录入1----7,分别于控制台输出对应的周一,周二,周三,周四,周五,周六,周天. (二)设置一个数组,求出数组中对应的最大值以及索引. (三)在控制台输出9x9乘法口诀表. (四)使用冒泡 ...
- iOS—网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据
网络爬虫-使用java语言抓取网络数据 前提:熟悉java语法(能看懂就行) 准备阶段:从网页中获取html代码 实战阶段:将对应的html代码使用java语言解析出来,最后保存到plist文件 上一 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- javaScript 添加和移除class类名的几种方法
添加类属性: // 一次只能设置一个类值,如果当前属性本身存在类值,会被替换 element.className = '类名'; /* * .setAttribute 用来设置自定义属性和值的 * 自 ...
- 【Kafka】JavaAPI操作
目录 先创建Maven工程导入jar包 Producer API Consumer API Stream API 先创建Maven工程导入jar包 帮助文档地址:http://kafka.apache ...
- Jmeter5.2.1 三种参数化
Jmeter的相关操作,一定要会参数化,常见的参数化方法有如下几种: 一.用户定义的变量 常应用于设置一些全局变量,包括url.host.port等等 线程组--添加--配置元件--用户定义的变量,添 ...
- dumpsys-package
dumpsys-package ams和pms是android系统最重要的系统服务,本文解析dumpsys package命令,看哪些PMS相关的系统信息,数据结构是运行时可以查看的. 命令提示 co ...
- STM32 TIM高级定时器死区时间的计算
STM32 TIM高级定时器的互补PWM支持插入死区时间,本文将介绍如何计算以及配置正确的死区时间. 文章目录 什么是死区时间? 数据手册的参数 如何计算合理的死区时间? STM32中配置死区时间 什 ...
- PC端软件配置
一,cmder软件安装 二,Snipaste软件安装 三,vwmare虚拟机安装 四,sublime安装 五,notepad++安装 六,Python环境 anaconda navigator安装 七 ...
- wpf winform 截图
wpf 通过下面的截图,标题可能会丢失. public void CreateBitmapFromVisual(Window win, string fileName) { ...
- 使用windows(win7和win10),最好用chocolatey
Win10平台使用PowerShell命令行choco来安装所需开源软件. 步骤如下: 打开Chocolatey 官方网站,The package manager for windows,这很巨硬. ...
- git命令之切换分支
Git一般有很多分支,我们clone到本地的一般都是master分支,如何进行分支的切换呢?那么下面带大家简单的看看如何通过命令来切换: 1.查看远程仓库及本地的所有分支 命令:git branch ...
- Nginx服务器的安装和卸载
Nginx的安装 安装Nginx之前,需要先获取Nginx的安装文件.我们可以在http://nginx.org/en/download.html获取各个版本的Nginx安装文件.大家可以按照自己的需 ...