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。

  1. class Solution {
  2. public static int maxValue=100000;
  3. public int networkDelayTime(int[][] times, int N, int K) {
  4. //构建邻接表,用于存放各个点到各个点的距离
  5. int[][] matrix=new int[N+1][N+1];
  6. for(int i=0;i<=N;i++){
  7. for(int j=0;j<=N;j++){
  8. matrix[i][j]=maxValue;
  9. }
  10. }
  11. //遍历times填充邻接表
  12. for(int[] time:times)
  13. matrix[time[0]][time[1]]=time[2];
  14. //存放 K 到各个点的最短路径,最大的那个最短路径即为结果
  15. int[] distance = new int[N + 1];
  16. //Arrays.fill(distance, -1);
  17. distance[K]=0;
  18. //判断是否找到K到达该点最短路径
  19. boolean[] visited = new boolean[N + 1];
  20. visited[K] = true;
  21. for(int i=1;i<=N-1;i++){
  22. int min=Integer.MAX_VALUE;
  23. int index=-1;
  24. for(int j=1;j<=N;j++){
  25. if(!visited[j] && matrix[K][j]<min){
  26. min=matrix[K][j];
  27. index=j;
  28. }
  29. }
  30. distance[index]=min;
  31. visited[index]=true;
  32. for(int k=1;k<=N;k++){
  33. if(!visited[k] && matrix[K][index]+matrix[index][k]<matrix[K][k]){
  34. matrix[K][k]=matrix[K][index]+matrix[index][k];
  35. }
  36. }
  37. }
  38. int maxDistance = 0;
  39. // 遍历最大值,如果有节点未被访问,返回 -1,否则返回最大最短路径
  40. for (int i = 1; i <= N; i++) {
  41. if (distance[i] ==maxValue) {
  42. return -1;
  43. }
  44. maxDistance = Math.max(distance[i], maxDistance);
  45. }
  46. return maxDistance;
  47. }
  48. }

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. 自定Tinymce中的图片上传

    在引入组件上添加 上传图片的url地址 <tinymce :height="300" ref="tinymce" //上传图片的url地址 upload- ...

  2. java web程序员微信群

    关注微信公众号"程序员成长日志",回复关键字"java"扫码进群 本群主要为大家解决工作中遇到的问题遇到的问题发到群里大家集思广益平时可以瞎扯不定期红包

  3. Linux下ffmpeg交叉编译

    1 获取源代码 git clone -b "branch" https://git.ffmpeg.org/ffmpeg.git "branch" 可以是以下的m ...

  4. python之邮件发送自动化

    # -*- coding:utf-8 -*-#@Time : 2020/3/24 22:55#@Autor: Mr.White#@File : 发送邮件.py 一.导入所需要的类 import smt ...

  5. SpringBoot基础实战系列(一)整合视图

    SpringBoot整合freemarker 1.添加依赖:springboot基本上是无缝衔接,基本上只需要添加对应的依赖,不需要或者做很少量的配置即可 注:对于springboot项目的创建此处不 ...

  6. vscode+eslint自动格式化vue代码的方法

    前言 使用vscode开发vue项目的时候,为了编码格式的统一化,使用eslint规范进行格式化.此时通过eslint插件可以实现对vue代码的自动格式化. 使用方式 在vscode的插件模块处,搜索 ...

  7. 2020年第二届“网鼎杯”网络安全大赛 白虎组 部分题目Writeup

    2020年第二届“网鼎杯”网络安全大赛 白虎组 部分题目Writeup 2020年网鼎杯白虎组赛题.zip下载 https://download.csdn.net/download/jameswhit ...

  8. SSH三大框架知识点

    Hibernate ****************************************************************************************** ...

  9. Java—CountDownLatch使用详解

    CountDownLatch介绍 CountDownLatch概述 CountDownLatch一般用作多线程倒计时计数器,强制它们等待其他一组(CountDownLatch的初始化决定)任务执行完成 ...

  10. 1.3Go环境搭建之Windows

    1.1.2. Golang SDK SDK 的全称(Software Development Kit 软件开发工具包) 2) SDK是提供给开发人员使用的,其中包含了对应开发语言的工具包 1.1.3. ...