原题链接在这里: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. yii框架中的下拉菜单和单选框

    yii中的下拉菜单: 第一种: <?= $form->field($model, 'parent_id')->dropDownList(ArrayHelper::map($data, ...

  2. Flutter — IDE Shortcuts for Faster Development

    https://medium.com/flutter-community/flutter-ide-shortcuts-for-faster-development-2ef45c51085b If yo ...

  3. crunch制作字典

    安装 安装crunch sudo apt-get install crunch 语法 crunch <min> max<max> <characterset> -t ...

  4. 纯css更改图片颜色的技巧

    tips: JPG.PNG.GIF 都可以,但是有一个前提要求,就是黑色纯色,背景白色 .pic1 {     background-image: url($img), linear-gradient ...

  5. PL/SQL的结构

    PL/SQL Developer是一个集成开发环境,专门开发面向Oracle数据库的应用.PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL).PL/S ...

  6. JAVA项目之增删改查

    public class ProductDao { // 查询所有商品 // BeanListHandler查询所有商品 public List<Product> getAll() thr ...

  7. wamp基本配置与设置外网访问

    wamp安装(都是一键安装)正常启动后,做一些基本配置的介绍: 1.打开rewrite_module,方法一:左键点击wamp图标,鼠标移至Apache,然后平移至Apache模块,勾选rewrite ...

  8. Cypress自动化环境搭建

    1.Cypress 下载: 官网下载,下载后直接解压即可,解压后便可单机exe文件打开 Ps:直接打开exe是会报错找不到json文件的,所以还要安装依赖环境 运行cypress项目前,必须vue-c ...

  9. angularcli 第七篇(service 服务)

    在组件中定义的信息是固定的,假设另外一个组件也需要用到这些信息,这时候就用到服务,实现 共享数据 和 方法 组件不应该直接获取或保存数据,它们不应该了解是否在展示假数据. 它们应该聚焦于展示数据,而把 ...

  10. session和cookie的区别和联系详解,Cookie Session相关看这篇就够了。

    本文转自:91博客:原文地址:http://www.9191boke.com/199015867.html 有一朋友做面试官的时候,曾经问过很多朋友这个问题: Cookie 和 Session 有什么 ...