当前位置:网站首页>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模板
边栏推荐
- A detailed explanation of what is software deployment
- 重构指标之如何监控代码圈复杂度
- 【TA-霜狼_may-《百人计划》】美术2.7 Metallic 与 Speculer流程
- 【愚公系列】2022年07月 Go教学课程 028-函数小结案例(通讯录)
- Task Computing【动态规划_牛客】
- 【Idea设置运行参数无效】可能是...
- 如何防止重复下单?
- 面渣逆袭:MySQL六十六问,两万字+五十图详解
- 请问一下dms的跨阿里云账户 新增实例,是不是无法新增redis ?
- The electromagnetic compatibility EMC protection study notes
猜你喜欢
Projector reached the party benefits 】 【 beginners entry - brightness projection and curtain selection - from entry to the master
数据分析入门导读
What are the useful IT asset management platforms?
Li Mu's deep learning notes are here!
实战:10 种实现延迟任务的方法,附代码!
ITSM软件与工单系统的区别是什么?
吴恩达机器学习[9]-神经网络学习
吴恩达机器学习[11]-机器学习性能评估、机器学习诊断
MySQL当前读、快照读、MVCC
What is the difference between ITSM software and a work order system?
随机推荐
张乐:研发效能的黄金三角及需求与敏捷协作领域的实践|直播回顾
【已解决】allure无法生成json文件和AttributeError: module ‘allure‘ has no attribute ‘severity_level‘
初学爬虫笔记(收集数据)
云存储硬核技术内幕——(10)
【二叉树】根据描述创建二叉树
seaborn
Manacher(求解最长回文子串)
成员变量与局部变量的区别有哪些
【Jprofile 11.0 安装】
DocuWare Platform - Content Services and Workflow Automation Platform for Document Management (Part 1)
你一定从未看过如此通俗易懂的YOLO系列(从v1到v5)模型解读
【Gopher 学个函数】边学边练,简单为 Go 上个分
postman “header“:{“retCode“:“999999“
GPS satellite synchronization clock, NTP network synchronization clock, Beidou clock server (Jingzhun)
农产品期货开户哪家好??
查看每个数据库分配给了哪些用户权限,这个有接口吗
Tomato assistant downloading tomatoes
Redis持久化操作
MySQL当前读、快照读、MVCC
Beginner crawler notes (collecting data)