LeetCode #743 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
.
Note:
N
will be in the range[1, 100]
.K
will be in the range[1, N]
.- The length of times will be in the range
[1, 6000]
. - All edges
times[i] = (u, v, w)
will have1 <= u, v <= N
and1 <= 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相关推荐
- LeetCode 743. Network Delay Time
原题链接在这里:https://leetcode.com/problems/network-delay-time/ 题目: There are N network nodes, labelled 1 ...
- leetcode 743. Network Delay Time | 743. 网络延迟时间(邻接矩阵,Dijkstra 算法)
题目 https://leetcode.com/problems/network-delay-time/ 题解 有向图,求源点到所有顶点的最短距离,经典 Dijkstra 算法,只要知道思路就能实现, ...
- 743. Network Delay Time
743. Network Delay Time 思路:其实就是求从一点出发到各个点的距离,最大网络延迟就是一个点到各个点的最大距离. 使用狄杰斯特拉求出一个点到各个点的距离,然后取最大那个值. 难点: ...
- Dijkstra算法求最短路径(附leetcode 743 网络延迟问题)
算法步骤:(设图的储存方式为邻接矩阵) 设置[dist数组](distance的缩写),dist[x]=y表示从[源点]到[x]的最短距离为y 设置[visited数组],visited[i]=Tru ...
- Network Delay Simulator模拟延时、带宽甚至丢包率,更精确地模拟慢网速环境
Network Delay Simulator 免费软件,下载地址 .我正在使用的,三种之中功能最强大,监听Network Interface Card (NIC)和TCP/IP stack之间的网络 ...
- Docker Issue Network Delay(自定义网络启动会延迟大概40秒!)
Docker Issue Network Delay 在用自定义Docker网络跑容器的时候发现一个问题:Docker的自定义网络启动会延迟大概40秒! 换句话说就是: 如果你使用自定义网络在一个容器 ...
- 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 ...
- LeetCode 743. 网络延迟时间(最短路径)
文章目录 1. 题目 2. 解题 2.1 弗洛伊德 1. 题目 有 N 个网络节点,标记为 1 到 N. 给定一个列表 times,表示信号经过有向边的传递时间. times[i] = (u, v, ...
- taoqick 搜索自己CSDN博客
L1 L2正则化和优化器的weight_decay参数 kaiming初始化的推导 Pytorch动态计算图 Pytorch自动微分机制 PyTorch中在反向传播前为什么要手动将梯度清零? 通俗讲解 ...
最新文章
- 害怕离职,侧面说明大多数是离职了没人要的废物?
- linux 系统运行状况 shell命令 watch 监控进程是否存在
- python 默认字典
- Django资源大全
- Netty实战 IM即时通讯系统(二)Netty简介
- 二维map —— HDU1263
- 大黑书《离散数学及其应用》之Dijkstra算法
- 熊海cms v1.0 SQL注入漏洞复现
- SubType.prototype.constructor = SubType原因
- 如何截图一张完整的表_wps2016表格太长怎么截图打印?
- Python爬虫编程思想(82):管理SQLite数据库
- 美丽离岛兰屿印象图集
- 虚拟机canal-deployer连接主机mysql失败
- 精油商城小程序,让您的品牌更出彩!
- 过压保护电路(OVP)
- SketchUp: Modeling Exteriors from Photos SketchUp:从照片建模外部 Lynda课程中文字幕
- Android开发常用代码
- 练习2-4:重新编写函数squeeze(s1,s2),将字符串s1中的任何字符与字符串时s2中的字符匹配的字符都删除
- Qt 通过条件编译区分Debug和Release代码
- 在项目中使用PageHelper实现分页功能
热门文章
- 【腾讯Bugly干货分享】聊聊苹果的Bug - iOS 10 nano_free Crash
- 摄像头P2P软件提供,完美解决打洞及音视频、用户码传输问题。
- 移动安全规范 — 2 -蓝牙安全规范
- java抢购_java redis 实现抢购秒杀
- mysql grant命令详解_MySQL授权命令grant的使用方法详解
- JavaScript实现拖动图片到指定位置验证码
- 关于onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
- 蓝牙耳机连Stereo mode上win10后没有声音?
- 《结构化思维》读书笔记
- python之pexpect实现自动交互