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经典例题)的更多相关文章

  1. Java之线程通信的应用:经典例题:生产者/消费者问题

    /** * 线程通信的应用:经典例题:生产者/消费者问题 * * 生产者(Productor)将产品交给店员(Clerk),而消费者(Customer)从店员处取走产品, * 店员一次只能持有固定数量 ...

  2. Java攻城狮之基础练习题------经典例题

    (一)键盘录入1----7,分别于控制台输出对应的周一,周二,周三,周四,周五,周六,周天. (二)设置一个数组,求出数组中对应的最大值以及索引. (三)在控制台输出9x9乘法口诀表. (四)使用冒泡 ...

  3. iOS—网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据

    网络爬虫-使用java语言抓取网络数据 前提:熟悉java语法(能看懂就行) 准备阶段:从网页中获取html代码 实战阶段:将对应的html代码使用java语言解析出来,最后保存到plist文件 上一 ...

  4. 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 ...

  5. 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. ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 微信小程序使用GoEasy实现websocket实时通讯

    不需要下载安装,便可以在微信好友.微信群之间快速的转发,用户只需要扫码或者在微信里点击,就可以立即运行,有着近似APP的用户体验,使得微信小程序成为全民热爱的好东西~ 同时因为微信小程序使用的是Jav ...

  2. java 8 stream、lambda表达式对list操作分组、过滤、求和、最值、排序、去重

    1.分组 通过groupingBy分组指定字段 list.stream().collect(Collectors.groupingBy(User::getSex)); 2.过滤 通过filter方法过 ...

  3. CSS理论:margin-left在float中的运用

    源码如下: margin-left 指的是左边的外边距,为正数时,左边间距增大,div向右偏移,为负数时,左边间距减少,相反往左偏移 双飞翼 .wrap { width: 100%; margin: ...

  4. python--制作微信好友照片墙

    知识来源:https://zhuanlan.zhihu.com/p/73975013 1.环境 os:MAC tool:python 3.7 ,pip3.7 2.前提: 使用pip3.7 instal ...

  5. 【白嫖】IT笔试面试真题讲解系列文章+视频

    视频讲解IT公司面试的高频考题~ 持续更新中,欢迎点赞转发~ 干货|名企高频考点指令篇-查看Linux硬盘空间使用情况 干货 | 名企高频考点指令篇-Linux查看CPU内存和系统版本 干货 | 名企 ...

  6. 读懂操作系统(x86)之堆栈帧(过程调用)

    前言 为进行基础回炉,接下来一段时间我将持续更新汇编和操作系统相关知识,希望通过屏蔽底层细节能让大家明白每节所阐述内容.当我们写下如下C代码时背后究竟发生了什么呢? #include <stdi ...

  7. hdu2138 How many prime numbers 米勒测试

    hdu2138 How many prime numbers #include <bits/stdc++.h> using namespace std; typedef long long ...

  8. 自己用C语言写NXP S32K144 serial bootloader

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader) 欢迎咨询或定制bootloader(在线升级程序). NXP S32K144 ...

  9. 必须使用角色管理工具 安装或配置microsoft.net framework 3.5

    windows server 2008安装sql server 2012后报错,要求安装microsoft.net framework 3.5 sp1 但安装时提示,必须使用角色管理工具 安装或配置m ...

  10. 马兴德201771010117《面向对象程序设计(java)》第一周学习总结

    第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com 挽歌朽年 程序设计评测:https://pintia.cn/ 791683057@qq ...