【LeetCode】743. Network Delay Time 解题报告(Python)
【LeetCode】743. Network Delay Time 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/network-delay-time/description/
题目描述:
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
.
题目大意
求单源有向图的最长路径。如果有节点不可抵达,返回-1.
解题方法
Dijkstra算法,纯模板题。
时间复杂度是O(N ^ 2 + E),空间复杂度是O(N+E).
代码如下:
class Solution:
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
K -= 1
nodes = collections.defaultdict(list)
for u, v, w in times:
nodes[u - 1].append((v - 1, w))
dist = [float('inf')] * N
dist[K] = 0
done = set()
for _ in range(N):
smallest = min((d, i) for (i, d) in enumerate(dist) if i not in done)[1]
for v, w in nodes[smallest]:
if v not in done and dist[smallest] + w < dist[v]:
dist[v] = dist[smallest] + w
done.add(smallest)
return -1 if float('inf') in dist else max(dist)
Floyd-Warshall算法。这个算法TLE.
时间复杂度O(n^3), 空间复杂度O(n^2)。
class Solution:
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
d = [[float('inf')] * N for _ in range(N)]
for time in times:
u, v, w = time[0] - 1, time[1] - 1, time[2]
d[u][v] = w
for i in range(N):
d[i][i] = 0
for k in range(N):
for i in range(N):
for j in range(N):
d[i][j] = min(d[i][j], d[i][k] + d[k][j])
return -1 if float('inf') in d[K - 1] else max(d[K - 1])
Bellman-Ford算法,这个算法TLE。
时间复杂度O(ne), 空间复杂度O(n)。
class Solution:
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
dist = [float('inf')] * N
dist[K - 1] = 0
for i in range(N):
for time in times:
u = time[0] - 1
v = time[1] - 1
w = time[2]
dist[v] = min(dist[v], dist[u] + w)
return -1 if float('inf') in dist else max(dist)
参考资料:
https://zxi.mytechroad.com/blog/graph/leetcode-743-network-delay-time/
https://leetcode.com/problems/network-delay-time/discuss/172857/Python-Dijkstra-Solution-that-beats-86
日期
2018 年 9 月 27 日 ———— 今天起得格外早
【LeetCode】743. Network Delay Time 解题报告(Python)的更多相关文章
- [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
原题链接在这里:https://leetcode.com/problems/network-delay-time/ 题目: There are N network nodes, labelled 1 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
随机推荐
- mysql—MySQL数据库中10位时间戳转换为标准时间后,如何对标准时间进行加减X天处理
在这篇的缘由:问题:"FROM_UNIXTIME(timeline,'%Y-%m')"的结果(2020-06)做月份增加1月或者减少1月的计算处理,想着直接在结果上+1但是,结果为 ...
- EXCEL-排名前三名显示小红旗,后三名显示小黑旗
总结(用的WPS):第一步:用=IF(RANK(数值,引用范围) <=3,"小红旗",IF(RANK(数值,引用,1) <=3,"小黑旗",&quo ...
- Python与Perl的相似与差别
Python version 3.7版本 00.命令行交互 命令行交互 Perl Python perl -e <Perl代码> #Unix/Linux/Windows/DOS 直 ...
- 用jquery的prop方法操作checkbox
prop设置checkbox选中 $('#checkbox-id').prop("checked",true) 判断checkbox是否选中,if ($('#checkbox-id ...
- php操作mongodb手册地址
php操作mongodb手册地址: http://php.net/manual/zh/class.mongocollection.php
- 巩固javaweb第十五天
巩固内容: 单选按钮: 在注册功能中,用户选择学历使用的是单选按钮,并且是多个单选按钮,每个选项对 应一个单选按钮,用户只能选择其中一个,这多个单选按钮的格式相同.如果用户要输入 的信息只有少数几种可 ...
- The Go tools for Windows + Assembler很好玩
我想用python做个tiny BASIC编译器.赋值和加减乘除,IF和FOR. 语法分析python有ply包,用ply.lex和ply.yacc做个计算器很简单,我已经做了. 做个解释器应该也不难 ...
- Give You My Best Wishes
亲耐滴IT童鞋们: 感谢大家一直以来的支持,因为有你们的支持,才有我这么"拼"的动力!!爱你们哟 OC的学习已经告一段落,希望大家通过阅读这几篇浅薄的随笔,能够寻找到解决问题的方法 ...
- OC-私有方法,构造方法,类的本质及启动过程
总结 标号 主题 内容 一 OC的私有方法 私有变量/私有方法 二 @property 概念/基本使用/寻找方法的过程/查找顺序 三 @synthesize @synthesize概念/基本使用/注意 ...
- JConsole可视化工具
JConsole基本介绍 Jconsole (Java Monitoring and Management Console),一种基于JMX的可视化监视.管理工具.JConsole 基本包括以下基本功 ...