当前位置:网站首页>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模板
边栏推荐
猜你喜欢
随机推荐
不需要服务器,教你仅用30行代码搞定实时健康码识别
Summary of some pytorch knowledge points that have been updated for a long time
进程间通信方式
Does DMS have an interface to get the list of databases under each instance?
招募 | 香港理工大学Georg Kranz 博士诚招博士
张乐:研发效能的黄金三角及需求与敏捷协作领域的实践
07-输入输出系统
弄懂#if #ifdef #if defined
不需要服务器,教你仅用30行代码搞定实时健康码识别
GET 和 POST 请求的区别
It took half a month to finally make a collection of high-frequency interview questions of first-tier manufacturers
【Pick-in】Advertising-information flow cross-domain CTR estimation (to be updated)
把boot和APP一起烧录进MCU
一文详解什么是软件部署
Go Go 简单的很,标准库之 fmt 包的一键入门
What is the difference between member variable and local variable
攻防视角下,初创企业安全实战经验分享
界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能
吴恩达机器学习[13]-支持向量机
【愚公系列】2022年07月 Go教学课程 028-函数小结案例(通讯录)
![吴恩达机器学习[9]-神经网络学习](/img/07/0eeb3cd5f3ea7c2baeec1732ea8d9a.png)







![吴恩达机器学习[11]-机器学习性能评估、机器学习诊断](/img/99/179c4c2db2b6c1edb61f129d46f313.png)