当前位置:网站首页>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模板
边栏推荐
- 《2022 年上半年全球独角兽企业发展研究报告》发布——DEMO WORLD世界创新峰会圆满落幕
- Real-Time Rendering 4th related resource arrangement (no credit required)
- Manacher(求解最长回文子串)
- 7 天学个Go,Go 结构体 + Go range 来学学
- GPS卫星同步时钟,NTP网络同步时钟,北斗时钟服务器(京准)
- 阿尔萨斯监控平台&普罗米修斯监控平台对服务器资源的监控
- H5 之 文件流转base64下载
- How to monitor code cyclomatic complexity by refactoring indicators
- Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
- 《电磁兼容防护EMC》学习笔记
猜你喜欢

postman “header“:{“retCode“:“999999“

Redis的主从复制和集群

The electromagnetic compatibility EMC protection study notes

B 站又上热搜了, HR 称「核心用户都是 Loser」

Real-Time Rendering 4th相关资源整理(无需积分 传火)

多商户商城系统功能拆解24讲-平台端分销会员

邮差"头":{“retCode”:“999999”

成功 解决 @keyup.enter=“search()“ 在el-input 组件中不生效的问题

有哪些好用的IT资产管理平台?

ICDE‘22推荐系统论文之Research篇
随机推荐
【伸手党福利】投影仪初学者入门——投影亮度及幕布选择——从入门到精通
屏幕分辨率兼容性
Tomato assistant downloading tomatoes
皕杰报表配置文件report_config.xml里都配置了什么?
NFT blind box mining system dapp development NFT chain game construction
js判断一个对象是否在一个对象数组中
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃
勒索软件的原理
云存储硬核技术内幕——(11) 女子会所的秘密
Typora收费?搭建VS Code MarkDown写作环境
An article to answer what is the product library of the DevOps platform
Go Go 简单的很,标准库之 fmt 包的一键入门
RepVGG学习笔记
DocuWare平台——用于文档管理的内容服务和工作流自动化的平台(上)
番茄插件番茄助手下载
In-depth analysis of HyperBDR cloud disaster recovery 1: Cloud-native cross-platform disaster recovery, making data flow more flexible
进程间通信方式
uni-app之renderjs
Task Computing【动态规划_牛客】
Pulsar消费者处理不当导致的消息积压问题