当前位置:网站首页>leetcode:787. K 站中转内最便宜的航班【k步最短路 + dfs记忆化 + defaultdict(dict)】
leetcode:787. K 站中转内最便宜的航班【k步最短路 + dfs记忆化 + defaultdict(dict)】
2022-06-30 15:49:00 【白速龙王的回眸】

ac code
class Solution:
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
# k站中转,可以走k + 1次
connect = defaultdict(dict) # 二维dict
for x, y, c in flights:
connect[x][y] = c
# 只能走k步的dfs
# 自顶向下
@cache
def dfs(now, remain):
# 成功退出
if now == dst:
return 0
# 到达步数,无限远
if remain == 0:
return inf
remain -= 1
res = inf
# 遍历下一个位置
for nxt in connect[now]:
res = min(res, dfs(nxt, remain) + connect[now][nxt])
return res
ans = dfs(src, k + 1)
return ans if ans != inf else -1
分析
用一个defaultdict记录每一个点能飞到的地方和距离
自顶向下的dfs + cache
记忆化参数是当前的位置以及剩余的次数,结果是到达终点的距离
如果能到达终点,返回0
如果次数用尽,返回inf
次数-1,且当前res设置为inf
遍历所有能飞的下一个位置,然后res取res和dfs(nxt, remain) + connect[now][nxt]的最小值
这种思想就是把问题推到下一步,直到尽头(到达终点,次数用尽)
总结
dfs解决k步最短路问题,优雅
边栏推荐
- Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
- 声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏
- Sub chain cross technology source level exploration: an overview of xcvm
- 《网络是怎么样连接的》读书笔记 - 汇总篇
- The 25th anniversary of Hong Kong's return to China the Hong Kong Palace Museum officially opened as a new cultural landmark
- 【微信小程序】常用组件基本使用(view/scroll-view/swiper、text/rich-text、button/image)
- Jspreadsheet/ce JExcel: more data fields than the given fields (columns) will lead to blank columns. Solution
- Cmakelists Basics
- 2022 Blue Bridge Cup group B -2022- (01 backpack to calculate the number of schemes)
- [demo] write file circularly
猜你喜欢

阿里云盘分享压缩包

Construction schedule of intelligent management and control platform in Shanxi Chemical Industry Park

Mathematical modeling for war preparation 33- grey prediction model 2

JS Es5 can also create constants?

AVIC UAV technology innovation board is listed: the fist product with a market value of 38.5 billion is pterodactyl UAV

JS ES5也可以创建常量?

华为帐号多端协同,打造美好互联生活

JSR303以及常见Validator实现

“推广+搞笑剧情”,如何碰撞出爆款的火花?

【JVM】一文带你了解JVM中的垃圾回收机制(GC)——内含图解
随机推荐
A scheduled task deletes data at a specified time
9: Chapter 3: e-commerce engineering analysis: 4: [general module]; (to be written...)
Parler du télétravail
RTP sending PS stream zero copy scheme
Mathematical modeling for war preparation 33- grey prediction model 2
List becomes vector list becomes vector list vector
深度学习——(2)几种常见的损失函数
jspreadsheet/CE JExcel数据字段比给的字段(columns)多会导致空白列的问题解决方案
【JVM】类加载相关面试题——类加载过程、双亲委派模型
addmodule_ allmerge_ ams_ im
Research on helmet wearing detection algorithm
Compile u-boot source code for stm32p157 development board
《网络是怎么样连接的》读书笔记 - 汇总篇
定时任务删除指定时间的的数据
[JVM] takes you to learn about the garbage collection mechanism (GC) in the JVM -- including diagrams
Nichenet actual silicosis
7 月 2 日邀你来TD Hero 线上发布会
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
Data security compliance has brought new problems to the risk control team
Geo read single cell CSV expression matrix single cell column name change Seurat