leetcode743 Network Delay Time
"""
here 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
"""
"""
此题很经典,是求源点到目标结点的最短路径
Dijkstra算法,纯模板题。
时间复杂度是O(N^2 + E),空间复杂度是O(N+E).
"""
class Solution1:
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 #K-1是存到距离数组的下标
for i in range(N): #N个结点,遍历N遍更新最短路径
for time in times:
u = time[0] - 1 #减1是为了dist的下标一致
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) ss = Solution1()
ss.networkDelayTime([[1, 2, 1], [2, 3, 7], [1, 3, 4], [2, 1, 2]], 3, 2)
leetcode743 Network Delay Time的更多相关文章
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- [Swift]LeetCode743. 网络延迟时间 | Network Delay Time
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...
- [LeetCode] Network Delay Time 网络延迟时间
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...
- [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 ...
- 743. Network Delay Time
题目来源: https://leetcode.com/problems/network-delay-time/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: class Solution: ...
- [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】Network Delay Time
题目: There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edge ...
- 排队时延(Queuing delay)
网络时延的构成 Network delay including four parts: Processing delay - time routers take to process the pack ...
随机推荐
- "%Error opening tftp://255.255.255.255/network config"
问题:服务配置错误消息(Service Configuration Error Messages) 有时,在通过Cisco IOS软件启动Cisco设备期间,会显示与这些类似的错误消息: %Error ...
- ANSYS 非线性材料模型简介1 ---常用弹塑性模型
目录 1. 材料非线性 2. 三个准则 2.1 屈服准则 2.2 流动准则 2.3 强化准则 3. 常用弹塑性模型 3.1 双线性等向强化 3.2 多线性等向强化 3.3 非线性等向强化 3.4 双线 ...
- 【原】rsync使用
在使用jenkins当跳板机的场景下,有使用git pull 代码到jenkins机器后,需要将代码复制到另一台机器上,常用的复制命令有scp和rsync:现就使用到了rsync进行详解: rsync ...
- 收藏---wordpress搭建出来的blog
http://blog.luofei.org/2012/02/painters-and-paintings-through-the-eyes-of-faith/
- Win Tomcat8 占用内存过高
1.解压版 找到tomcat/bin/catalina.bat 文件,修改对应参数 2.安装版 windows服务执行的是bin/tomcat.exe.他读取注册表中的值,而不是catalina.ba ...
- php 低版本不能使用php 命令,创建软链接
ln -s /usr/local/php5/bin/php /usr/bin/php php 低版本不能使用php 命令,创建软链接 phpize 依赖于 phpcli 模式 所以php命令必 ...
- FreeSWITCH调用第三方TTS 使用tts_commandline
FreeSWITCH 支持调用第三方TTS命令,本身已经搭好了框架,只需要配置即可用. 下面写一下步骤,以免忘记,也希望给你带来帮助. 第一步:编译模块mod_tts_commandline,并加载. ...
- android 支持上拉加载,下拉刷新的列表控件SwipeRefreshLayout的二次封装
上拉加载,下拉刷新的列表控件,大家一定都封装过,或者使用过 源代码,我会在最后贴出来 这篇代码主要是为了解决两个问题 1.滑动冲突得问题 2.listview无数据时,无数据布局的展示问题 下方列出的 ...
- iOS马甲包上架总结
https://www.jianshu.com/p/da0a259338ea iOS马甲包上架首先明白一点,这个上架的app马甲包一定是不合规的.不然也不会使用马甲包上架. 上架过程中遇到的坑. 因为 ...
- 笔记-Python-性能优化
笔记-Python-性能优化 1. 开始 1.1. python性能差么? 做一个判断前,先问是不是. python运行效率低是事实. 1.2. 为什么? 原因: Python是 ...