当前位置:网站首页>leetcode:743. 网络延迟时间【单源最短路 + dijkstra模板】
leetcode:743. 网络延迟时间【单源最短路 + dijkstra模板】
2022-08-04 15:58:00 【白速龙王的回眸】
分析
根据dijkstra的过程,可以求出k到每个点的最短传递时间
而且是依次找最短的,只要返回最大的dist即可
ac code
class Solution:
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
# dijkstra(单源最短路)
g = [[inf] * n for _ in range(n)]
for u, v, w in times:
g[u - 1][v - 1] = w
dist = [inf] * n
visit = [False] * n
# init
dist[k - 1] = 0
for _ in range(n):
# nextMinDist
x = -1
for y, v in enumerate(visit):
if not v and (x == -1 or dist[y] < dist[x]):
x = y
# update
visit[x] = True
for y, v in enumerate(dist):
if not visit[y]:
dist[y] = min(dist[y], dist[x] + g[x][y])
ans = max(dist)
return ans if ans < inf else -1
总结
dijkstra模板
边栏推荐
- JVM Tuning-GC Fundamentals and Tuning Key Analysis
- It took half a month to finally make a collection of high-frequency interview questions of first-tier manufacturers
- 视频字幕API接口文档
- DevOps平台中的制品库是什么?有什么用处?
- 【Go事】一眼看穿 Go 的集合和切片
- 微信小程序获取年月日周及早上、中午、晚上
- 软考 --- 软件工程(2)软件开发方法
- Tinymce plugins [Tinymce 扩展插件集合]
- MetaAI科学家解读最新模型:200+语言互译,扩充千倍翻译数据,全球元宇宙用户自由交流
- 07-输入输出系统
猜你喜欢
多商户商城系统功能拆解24讲-平台端分销会员
可视化大屏丑?这篇文章教你如何做美观大屏!
What is the difference between ITSM software and a work order system?
B 站又上热搜了, HR 称「核心用户都是 Loser」
#夏日挑战赛# HarmonyOS 实现一个滑块验证
界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能
What are the useful IT asset management platforms?
项目里的各种配置,你都了解吗?
How to monitor code cyclomatic complexity by refactoring indicators
To ensure that the communication mechanism
随机推荐
现代 ABAP 编程语言中的正则表达式
实战:10 种实现延迟任务的方法,附代码!
学 Go,最常用的技能是什么?打日志
如何防止重复下单?
使用百度EasyDL实现森林火灾预警识别
全差分运放:THS4140
花了半个月,终于把一线大厂高频面试题做成合集了
张乐:研发效能的黄金三角及需求与敏捷协作领域的实践
Projector reached the party benefits 】 【 beginners entry - brightness projection and curtain selection - from entry to the master
MySQL当前读、快照读、MVCC
B 站又上热搜了, HR 称「核心用户都是 Loser」
项目里的各种配置,你都了解吗?
吴恩达机器学习[12]-机器学习系统设计
可视化大屏丑?这篇文章教你如何做美观大屏!
【TA-霜狼_may-《百人计划》】美术2.7 Metallic 与 Speculer流程
现代 ABAP 编程语言中的正则表达式
[TA-Frost Wolf_may-"Hundred Talents Project"] Art 2.7 Metallic and Speculer Process
录音文件识别
平稳发展 | 西欧地区手游玩家的数据和洞察
H5 开发内嵌页面跨域问题