当前位置:网站首页>leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]
leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]
2022-06-30 17:36:00 【Review of the white speed Dragon King】

ac code
class Solution:
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
# k Transfer station , You can go k + 1 Time
connect = defaultdict(dict) # A two-dimensional dict
for x, y, c in flights:
connect[x][y] = c
# Can only go k Step by step dfs
# The top-down
@cache
def dfs(now, remain):
# Quit successfully
if now == dst:
return 0
# Arrival steps , infinity
if remain == 0:
return inf
remain -= 1
res = inf
# Traverse the next position
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
analysis
Use one defaultdict Record where and how far each point can fly
Top down dfs + cache
The memory parameters are the current position and the number of times remaining , The result is the distance to the destination
If we can reach the end , return 0
If you run out of times , return inf
frequency -1, And the current res Set to inf
Traverse all the next positions that can fly , then res take res and dfs(nxt, remain) + connect[now][nxt] The minimum value of
This idea is to push the problem to the next step , To the end ( Reach the end point , Run out of times )
summary
dfs solve k Step shortest path problem , grace
边栏推荐
- Horizontal visual error effect JS special effect code
- [proteus simulation] Arduino uno uses 74ls148 to extend interrupt
- 面试突击60:什么情况会导致 MySQL 索引失效?
- Key to understanding the trend of spot Silver
- parker比例溢流阀RS10R35S4SN1JW
- 每日刷题记录 (九)
- If your MES is not upgraded, it will be eliminated
- 小程序容器与物联网结合的方式
- 【C语言】详解线程 — 通过 “加锁” 解决并发程序引起的共享内存问题
- Login box tricks
猜你喜欢

Parker Parker sensor p8s-grflx
![[untitled] write a student achievement and information management system in C language to realize the operation interface, clear screen display of current operation functions, reading and inputting st](/img/5f/cda69a34e93b3697992d576dbf0fae.jpg)
[untitled] write a student achievement and information management system in C language to realize the operation interface, clear screen display of current operation functions, reading and inputting st

k线图精解与实战应用技巧(见位进场)
![[零基础学IoT Pwn] 环境搭建](/img/3b/a0689a1570fcc40bb9a5a4e9cdc63c.png)
[零基础学IoT Pwn] 环境搭建

Exercise book of introduction to database system

登录框Tricks

知名互联网房屋租赁服务公司物联网关键业务迁移上云实践

AcWing 第 57 场周赛

编写C语言的最简单小程序Hello world

svg实现的订票UI效果
随机推荐
流批一体在京东的探索与实践
Plane intersection and plane equation
[C language] explain threads - thread separation function pthread_ detach
新技能:通过代码缓存加速 Node.js 的启动
Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution
Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
【C语言】详解线程 — 开启两个线程
6 張圖帶你搞懂 TCP 為什麼是三次握手?
24: Chapter 3: develop pass service: 7: user defined exceptions (to represent errors in the program); Create graceexceptionhandler to handle exceptions globally and uniformly (build JSON data of corre
Development: how to install offline MySQL in Linux system?
flutter 音乐 录音 播放 audioplayers
【剑指Offer】53 - I. 在排序数组中查找数字 I
送受伤婴儿紧急就医,滴滴司机连闯五个红灯
Key to understanding the trend of spot Silver
[零基础学IoT Pwn] 环境搭建
Exch: repair the missing system mailbox
New skill: accelerate node through code cache JS startup
Solution: STM32 failed to parse data using cjson
unity粒子_异常显示处理
Parker variable displacement piston pump pv092r1k1t1nmmc