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 是目标节点 ...
随机推荐
- Hive drop table,create table没有反应处理方法
Hive drop table时没有反应,于是强制中断. 解决之法,对其进行补充. mysql> show variables like 'char%';第一步:进入mysql,输入:show ...
- centos 安装htop
1.首先启用 EPEL Repository yum -y install epel-release 2.可以用 yum 直接安裝 Htop: yum -y install htop
- SessionChange
protected override void OnSessionChange(SessionChangeDescription changeDescription) { System.IO.File ...
- Typora基础
Typora下载网址https://typora.io 一级标题 :# 空格 编写内容 二级标题 2*# 空格 内容 typora快捷键 ctrl+1 =一级标题 有序内容 1.+tab (Q旁边的t ...
- css,对包含有子元素的元素进行flex后,会影响原有的布局。如何后续处理
对包含有子元素的元素进行flex后,会影响原有的布局. 例如设置两个div,第一个div包含一个img(图片),第二个div包含多个p元素(对前面的img的说明).如下图 1:当对着两个两个div进行 ...
- chrome浏览页面常用快捷键
1.chrome浏览页面常用快捷键 Ctrl+N 打开新窗口. Ctrl+T 打开新标签页. Ctrl+W关闭当前标签 Ctrl + F4 关闭chrome浏览器 Ctrl+Tab 或 Ctrl+Pg ...
- unity shader入门(四):高光
高光反射计算公式(phong模型)Cspecular=(Clight*Mspecular)max(0,v*r)mgloss mgloss为材质的官泽度,也成反射度,控制高光区域亮点有多大 Mspecu ...
- Linq 等式运算符:SequenceEqual(转载)
https://www.bbsmax.com/A/nAJvbKywJr/ 引用类型比较的是引用,需要自己实现IEqualityComparer 比较器. IList<string> str ...
- MySQL修炼之路三
1. SQL查询 1. 执行顺序 3. select ... 聚合函数 from 表名 1. where ... 2. group by ... 4. having ... 5. order by . ...
- BootstrapValidator 表单验证超详细教程
一. 引入js 和css文件 在有jquery和bootstrap的页面里引入 bootstrapValidator.js bootstrapValidator.css 链接: https://pan ...