题目

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.

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 1 <= w <= 100.

解题思路

最重要的就是理解题意,题目的意思是:当信号到达了一个结点时,在下一刻可以同时转发给该结点相邻的所有结点,求传送到每一个结点的最终时间。这样,问题就变成了求解从结点 K 到所有结点的单源最短路问题,然后,从中选择最长的一条路所用的时间即为答案。因此本题使用 Dijkstra 算法进行求解。需要注意的是每次从所有的路径中选择最短路径的结点出来后,要将该结点设置为已访问,避免再重复访问。

C++代码实现

class Solution {
public:int networkDelayTime(vector<vector<int>>& times, int N, int K) {vector<vector<int> > graph(N + 1, vector<int>(N + 1, 60001));vector<int> cost(N + 1);vector<bool> visited(N + 1, false); for (int i = 0; i < times.size(); ++i) { graph[times[i][0]][times[i][1]] = times[i][2]; }for (int i = 1; i <= N; ++i) { cost[i] = graph[K][i]; }cost[K] = 0;visited[K] = true;for (int i = 1; i <= N; ++i) {int minNode = findMinNode(cost, visited);if (minNode == 0) { break; }for (int j = 1; j <= N; ++j) {if (!visited[j] && cost[j] > cost[minNode] + graph[minNode][j]) {cost[j] = cost[minNode] + graph[minNode][j];}}visited[minNode] = true;}return calSum(cost);}int findMinNode(vector<int>& cost, vector<bool>& visited) {int minIndex = 0, minV = 60001;for (int i = 1; i < cost.size(); ++i) {if (!visited[i] && minV > cost[i]) { minIndex = i; minV = cost[i];}}return minIndex;}int calSum(vector<int>& cost) {int sum = 0;for (int i = 1; i < cost.size(); ++i) {if (cost[i] == 60001) { return -1; }if (sum < cost[i]) { sum = cost[i]; }}return sum;}
};

LeetCode #743 Network Delay Time相关推荐

  1. LeetCode 743. Network Delay Time

    原题链接在这里:https://leetcode.com/problems/network-delay-time/ 题目: There are N network nodes, labelled 1  ...

  2. leetcode 743. Network Delay Time | 743. 网络延迟时间(邻接矩阵,Dijkstra 算法)

    题目 https://leetcode.com/problems/network-delay-time/ 题解 有向图,求源点到所有顶点的最短距离,经典 Dijkstra 算法,只要知道思路就能实现, ...

  3. 743. Network Delay Time

    743. Network Delay Time 思路:其实就是求从一点出发到各个点的距离,最大网络延迟就是一个点到各个点的最大距离. 使用狄杰斯特拉求出一个点到各个点的距离,然后取最大那个值. 难点: ...

  4. Dijkstra算法求最短路径(附leetcode 743 网络延迟问题)

    算法步骤:(设图的储存方式为邻接矩阵) 设置[dist数组](distance的缩写),dist[x]=y表示从[源点]到[x]的最短距离为y 设置[visited数组],visited[i]=Tru ...

  5. Network Delay Simulator模拟延时、带宽甚至丢包率,更精确地模拟慢网速环境

    Network Delay Simulator 免费软件,下载地址 .我正在使用的,三种之中功能最强大,监听Network Interface Card (NIC)和TCP/IP stack之间的网络 ...

  6. Docker Issue Network Delay(自定义网络启动会延迟大概40秒!)

    Docker Issue Network Delay 在用自定义Docker网络跑容器的时候发现一个问题:Docker的自定义网络启动会延迟大概40秒! 换句话说就是: 如果你使用自定义网络在一个容器 ...

  7. linux模拟网络延迟,使用Nistnet搭建网络延迟模拟设备 (network delay simulator)

    mknod /dev/hitbox c 62 0 mknod /dev/nistnet c 62 1 chown root /dev/hitbox chown root /dev/nistnet mk ...

  8. LeetCode 743. 网络延迟时间(最短路径)

    文章目录 1. 题目 2. 解题 2.1 弗洛伊德 1. 题目 有 N 个网络节点,标记为 1 到 N. 给定一个列表 times,表示信号经过有向边的传递时间. times[i] = (u, v, ...

  9. taoqick 搜索自己CSDN博客

    L1 L2正则化和优化器的weight_decay参数 kaiming初始化的推导 Pytorch动态计算图 Pytorch自动微分机制 PyTorch中在反向传播前为什么要手动将梯度清零? 通俗讲解 ...

最新文章

  1. 害怕离职,侧面说明大多数是离职了没人要的废物?
  2. linux 系统运行状况 shell命令 watch 监控进程是否存在
  3. python 默认字典
  4. Django资源大全
  5. Netty实战 IM即时通讯系统(二)Netty简介
  6. 二维map —— HDU1263
  7. 大黑书《离散数学及其应用》之Dijkstra算法
  8. 熊海cms v1.0 SQL注入漏洞复现
  9. SubType.prototype.constructor = SubType原因
  10. 如何截图一张完整的表_wps2016表格太长怎么截图打印?
  11. Python爬虫编程思想(82):管理SQLite数据库
  12. 美丽离岛兰屿印象图集
  13. 虚拟机canal-deployer连接主机mysql失败
  14. 精油商城小程序,让您的品牌更出彩!
  15. 过压保护电路(OVP)
  16. SketchUp: Modeling Exteriors from Photos SketchUp:从照片建模外部 Lynda课程中文字幕
  17. Android开发常用代码
  18. 练习2-4:重新编写函数squeeze(s1,s2),将字符串s1中的任何字符与字符串时s2中的字符匹配的字符都删除
  19. Qt 通过条件编译区分Debug和Release代码
  20. 在项目中使用PageHelper实现分页功能

热门文章

  1. 【腾讯Bugly干货分享】聊聊苹果的Bug - iOS 10 nano_free Crash
  2. 摄像头P2P软件提供,完美解决打洞及音视频、用户码传输问题。
  3. 移动安全规范 — 2 -蓝牙安全规范
  4. java抢购_java redis 实现抢购秒杀
  5. mysql grant命令详解_MySQL授权命令grant的使用方法详解
  6. JavaScript实现拖动图片到指定位置验证码
  7. 关于onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
  8. 蓝牙耳机连Stereo mode上win10后没有声音?
  9. 《结构化思维》读书笔记
  10. python之pexpect实现自动交互