当前位置:网站首页>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步最短路问题,优雅
边栏推荐
- MySQL8 NDB Cluster安装部署
- 2022 Blue Bridge Cup group B -2022- (01 backpack to calculate the number of schemes)
- jspreadsheet/CE JExcel数据字段比给的字段(columns)多会导致空白列的问题解决方案
- Mathematical modeling for war preparation 35 time series prediction model
- [JVM] takes you to learn about the garbage collection mechanism (GC) in the JVM -- including diagrams
- RT thread heap size setting
- 山西化工园区智能化管控平台建设时间表
- AVIC UAV technology innovation board is listed: the fist product with a market value of 38.5 billion is pterodactyl UAV
- Drug management system plus database, overnight, plus report
- IndexSearch
猜你喜欢

MySQL8 NDB Cluster安装部署

声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏

Design of piece counter based on 51 single chip microcomputer

IO流_递归

Niuke network: longest continuous subarray with positive product

Etcd tutorial - Chapter 8 compact, watch, and lease APIs for etcd

redis淘汰策略

【微信小程序】小程序的宿主环境
![[JVM] class loading related interview questions - class loading process and parental delegation model](/img/4a/99fee19828b0c1234cc863db88fda1.png)
[JVM] class loading related interview questions - class loading process and parental delegation model

7 月 2 日邀你来TD Hero 线上发布会
随机推荐
Additional: (not written yet, don't look at ~ ~ ~) webmvcconfigurer interface;
addmodule_allmerge_ams_im
数据挖掘知识点整理(期末复习版)
restartProcessIfVisible的流程
Mathematical modeling for war preparation 33- grey prediction model 2
Jspreadsheet/ce JExcel: more data fields than the given fields (columns) will lead to blank columns. Solution
Dart: string replace related methods
列表变成向量 列表变向量 list vector
List becomes vector list becomes vector list vector
阿里云盘分享压缩包
补充材料 supplementary
Hologres共享集群助力淘宝订阅极致精细化运营
Differential analysis between different groups nichenet for silicosis runs successfully!
If your MES is not upgraded, it will be eliminated
[wechat applet] basic use of common components (view/scroll-view/wiper, text/rich-text, button/image)
互联网研发效能实践之去哪儿网(Qunar)核心领域DevOps落地实践
[JVM] takes you to learn about the garbage collection mechanism (GC) in the JVM -- including diagrams
Redis elimination strategy
声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏
Niuke.com: minimum cost of climbing stairs