LeetCode 743. Network Delay Time
原题链接在这里:https://leetcode.com/problems/network-delay-time/
题目:
There are N network nodes, labelled 1 to N.
Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the target node, and w is the time it takes for a signal to travel from source to target.
Now, we send a signal from a certain node K. How long will it take for all nodes to receive the signal? If it is impossible, return -1.
Example 1:

Input: times = [[2,1,1],[2,3,1],[3,4,1]], N = 4, K = 2
Output: 2
Note:
Nwill be in the range[1, 100].Kwill be in the range[1, N].- The length of
timeswill be in the range[1, 6000]. - All edges
times[i] = (u, v, w)will have1 <= u, v <= Nand0 <= w <= 100.
题解:
Construct a graph. Then use the shortest time to traverse to the point.
Have a minHeap based on the time spent to get to the point. With the graph and current point, find all the next points and calculate the time to get to that point, current.time + time from current to next point, put it to minHeap.
Thus, we could always get to a point with shortest time.
Note: Update visited set when poll out of minHeap but not before adding to the heap. Otherwise, if it takes very long time to next point and add the next point to visited, later if there is shorter path to that point, it can't be added to minHeap.
So update visited set after polling out of minHeap and update res if it is not visited before.
Time Complexity: O(E+VlogV). It takes O(E) time to construct graph. O(VlogV) time to traverse all the points.
Space: O(E+V). O(E) for graph, O(V) for minHeap and Set.
AC Java:
class Solution {
public int networkDelayTime(int[][] times, int N, int K) {
Map<Integer, List<int []>> graph = new HashMap<>();
for(int [] edge : times){
graph.putIfAbsent(edge[0], new ArrayList<int []>());
graph.get(edge[0]).add(new int[]{edge[1], edge[2]});
}
int res = 0;
PriorityQueue<int []> minHeap = new PriorityQueue<int []>((a,b) -> a[0]-b[0]);
minHeap.add(new int[]{0, K});
Set<Integer> visited = new HashSet<Integer>();
int count = 0;
while(!minHeap.isEmpty()){
int[] cur = minHeap.poll();
if(visited.contains(cur[1])){
continue;
}
visited.add(cur[1]);
count++;
res = cur[0];
if(graph.containsKey(cur[1])){
for(int [] next : graph.get(cur[1])){
minHeap.add(new int[]{res+next[1], next[0]});
}
}
}
return count == N ? res : -1;
}
}
LeetCode 743. Network Delay Time的更多相关文章
- [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 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 743. Network Delay Time
题目来源: https://leetcode.com/problems/network-delay-time/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: class Solution: ...
- 【leetcode】Network Delay Time
题目: There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edge ...
- [LeetCode] Network Delay Time 网络延迟时间
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...
- [LeetCode] Network Delay Time 网络延迟时间——最短路算法 Bellman-Ford(DP) 和 dijkstra(本质上就是BFS的迭代变种)
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...
- [Swift]LeetCode743. 网络延迟时间 | Network Delay Time
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...
- leetcode743 Network Delay Time
""" here are N network nodes, labelled 1 to N. Given times, a list of travel times as ...
- Java实现 LeetCode 743 网络延迟时间(Dijkstra经典例题)
743. 网络延迟时间 有 N 个网络节点,标记为 1 到 N. 给定一个列表 times,表示信号经过有向边的传递时间. times[i] = (u, v, w),其中 u 是源节点,v 是目标节点 ...
随机推荐
- Linux基础(06)IO复用
在Windows文件指的就是普通的肉眼可见的文件 , 而Linux一切皆文件 https://blog.csdn.net/nan_nan_nan_nan_nan/article/details/812 ...
- golang笔记之DOS篇
Dos的常用命令 dos的基本介绍 Dos: Disk Operating System 磁盘操作系统 ,简单说一下Windows下的目录 2. dos的基本操作原理 目录的操作: md ...
- python_并发与通信
独立的进程内存空间与共享的服务器进程空间 知识点一: 进程间通信的限制 进程是独立的,互不干扰的独立内存空间我们想不能修改变量但是,深层次问题是,这个进程与那个进程完全失去了联系 import mul ...
- SnowflakeIdWorker
/** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0 ...
- WPF 的命令的自动刷新时机——当你 CanExecute 会返回 true 但命令依旧不可用时可能是这些原因
原文:WPF 的命令的自动刷新时机--当你 CanExecute 会返回 true 但命令依旧不可用时可能是这些原因 在 WPF 中,你可以使用 Command="{Binding Walt ...
- repodata创建本地YUM仓库
参考一createrepo是linux下的创建仓库的软件包.create是创建的意思,repo是repository的缩写,是仓库的意思. yum(Yellow dog Updater,Modifie ...
- VUE回顾基础3
1.方法 在vue模板里函数被定义为方法来使用,将函数放在methods对象里,作为一个属性,就可以在模板里使用它 this:在方法中this指向该方法所属的组件,可以使用this方文档data对象的 ...
- 查找单链表中倒数第k个结点
本文转自:程序员面试题6--查找链表中倒数第k个结点 题目:输入一个单向链表,输出该链表中倒数第k个结点.链表的倒数第0个结点为链表的尾指针.链表结点定义如下: struct ListNode { i ...
- Oracle恢复流程图
本图来自于网络,想当初小麦苗刚开始接触备份恢复的时候,就是靠着这张图来学习的,今天把这张图分享给大家,共勉. ............................................. ...
- IDEA如何配置tomcat及建一个web项目
需求:项目工程可能别人已经建好了,我需要把项目导入到自己的IDEA并配置tomcat运行; 准备工作:1.本地的tomat服务器 2.IDEA工具 3.JDK 步骤: 1.点击Run -> Ed ...