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 ...
随机推荐
- 杭电2504 又见GCD
又见GCD Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- [运维] 如何解决 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
环境: 虚拟机 linux centos 7 64 当时正在配置 nginx , 由于解压后的 nginx 默认安装位置是在 /usr/local/ 目录下, 而这个目录是 root 用户才有权限操作 ...
- 第七届蓝桥杯javaB组真题解析-四平方和(第八题)
题目 /* 四平方和 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多4个正整数的平方和. 如果把0包括进去,就正好可以表示为4个数的平方和. 比如: 5 = 0^2 + 0^2 + 1 ...
- QT无法读入txt文件内容
用vs写QT无法利用相对路径读入txt文件,应将此文件加入到资源文件中.
- Maven打包项目失败;报错:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project Hello: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/we
报错信息: E:\MIKEY\mikey\HTML5\TestMaven_01>mvn package [INFO] Scanning for projects... [INFO] [INFO] ...
- day1-1JavaScript概念
Js概念: 产生于低速网时代,是进行表单验证 与html和css结合后产生动态效果(能用css实现的动画效果就不要用js实现,因为js效率比css低) js = ECMAScript + dom ...
- 十六 OGNL在Struts2环境的入门
一 配置核心过滤器
- 吴裕雄--天生自然python数据清洗与数据可视化:MYSQL、MongoDB数据库连接与查询、爬取天猫连衣裙数据保存到MongoDB
本博文使用的数据库是MySQL和MongoDB数据库.安装MySQL可以参照我的这篇博文:https://www.cnblogs.com/tszr/p/12112777.html 其中操作Mysql使 ...
- Netcat - 网络工具中的瑞士军刀
nc的一些小应用,慢更新.... 1.一个简单的聊天工具,Client1和Client2之间,Client1安装了nc,监听8888端口,Client2用telnet Client1的8888端口即可 ...
- 创建SSH keys用于添加到Git服务器上
SSH keys SSH key 可以让你在你的电脑和Git服务器之间建立安全的加密连接.先执行以下语句来判断是否已经存在本地公钥: cat ~/.ssh/id_rsa.pub 如果你看到一长串以 s ...