原题链接在这里: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:

  1. N will be in the range [1, 100].
  2. K will be in the range [1, N].
  3. The length of times will be in the range [1, 6000].
  4. All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 0 <= 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的更多相关文章

  1. [LeetCode] 743. Network Delay Time 网络延迟时间

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...

  2. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  3. 743. Network Delay Time

    题目来源: https://leetcode.com/problems/network-delay-time/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: class Solution: ...

  4. 【leetcode】Network Delay Time

    题目: There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edge ...

  5. [LeetCode] Network Delay Time 网络延迟时间

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...

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

  7. [Swift]LeetCode743. 网络延迟时间 | Network Delay Time

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...

  8. leetcode743 Network Delay Time

    """ here are N network nodes, labelled 1 to N. Given times, a list of travel times as ...

  9. Java实现 LeetCode 743 网络延迟时间(Dijkstra经典例题)

    743. 网络延迟时间 有 N 个网络节点,标记为 1 到 N. 给定一个列表 times,表示信号经过有向边的传递时间. times[i] = (u, v, w),其中 u 是源节点,v 是目标节点 ...

随机推荐

  1. 简单端口映射、转发、重定向工具-Rinetd

    一.简介 Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具.Rinetd是单一过程的服务器,它处理任何数量的连接到在配置文件etc/rinetd中指定的 ...

  2. 【leetcode-11】盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  3. Scala 系列(三)—— 流程控制语句

    一.条件表达式if Scala 中的 if/else 语法结构与 Java 中的一样,唯一不同的是,Scala 中的 if 表达式是有返回值的. object ScalaApp extends App ...

  4. Quartz基础调度框架-第一篇控制台

    Quartz基础调度框架 Quartz核心的概念:scheduler任务调度.Job任务.Trigger触发器.JobDetail任务细节 结构 Conf 在这个基本结构里 是用来存放配置 publi ...

  5. Linux系统新手入门学习的四点建议

    随着计算机的普及.互联网的发展,原本黑客手中的攻城利器---Linux,渐渐进入到普通群众的视线里,让越来越多的人接触到Linux,并学习Linux进而投身到Linux运维工作中去.如果大家对Linu ...

  6. idea下java项目的打包与使用

    一. 打包 (1)打开项目结构,选择Artifacts --> + --> JAR --> From modules with dependencies ... 有main方法就添加 ...

  7. kafka消费者问题

    [] 2019-12-17 15:40:01 - [INFO] [AbstractCoordinator:542 coordinatorDead] Marking the coordinator 机器 ...

  8. 英语dyamaund钻石

    dyamaund  英文词汇,中文翻译为金刚石的;镶钻;用钻石装饰 中文名:镶钻;钻石装饰 外文名:dyamaund 目录 释义 dyamaund 读音:[ˈdaɪəmənd, ˈdaɪmənd] ...

  9. 介绍一个免费的云开发工具:Cloud Shell

    上周和一德国同事吹牛的时候,他说最近业余时间在玩一个东东,叫做Cloud Shell,Google出品.Jerry之前听说过国内的阿里云也提供过类似的解决方案,即在云端提供一个受限制的Linux环境并 ...

  10. Linux添加硬盘创建新的逻辑卷方式

    有同仁看了上文<Linux添加硬盘扩充已有分区存储空间方式>一文后,提出疑问,现在很多云服务器本来没有逻辑卷,添加数据盘后需要自行添加,如何处理? 此文将以某云服务器为例,详细进行解说. ...