当前位置:网站首页>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
边栏推荐
- 万卷书 - 欧洲的门户 [The Gates of Europe]
- Share 5 commonly used feature selection methods, and you must see them when you get started with machine learning!!!
- [zero basic IOT pwn] environment construction
- Parker proportional overflow valve rs10r35s4sn1jw
- The gates of Europe
- [C language] explain threads - thread separation function pthread_ detach
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- js 从原型链到继承
- Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
- Shutter music recording playing audioplayers
猜你喜欢
开发那些事儿:如何在视频中添加文字水印?
【网易云信】播放demo构建:无法将参数 1 从“AsyncModalRunner *”转换为“std::nullptr_t”**
腾讯云的一场硬仗
[零基础学IoT Pwn] 环境搭建
力士乐液控单向阀Z2S10-1-3X/
ROC-RK3566-PC使用10.1寸IPS触摸屏显示
Compile - compile for itop4412 development board makefile
水平视觉错误效果js特效代码
Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution
SSH tool pyqt
随机推荐
Mysql8 NDB cluster installation and deployment
splitting.js密码显示隐藏js特效
Parker variable displacement piston pump pv092r1k1t1nmmc
分享 5 大常用的特征选择方法,机器学习入门必看!!!
Interview shock 60: what will cause MySQL index invalidation?
[零基础学IoT Pwn] 环境搭建
leetcode:787. K 站中转内最便宜的航班【k步最短路 + dfs记忆化 + defaultdict(dict)】
Write the simplest small program in C language Hello World
[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
Spin lock exploration
Nodejs learning notes II
Horizontal visual error effect JS special effect code
The new version of Shangding cloud | favorites function has been launched to meet personal use needs
Development: how to install offline MySQL in Linux system?
canvas鼠标控制重力js特效
[zero basic IOT pwn] environment construction
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
splitting. JS password display hidden JS effect
Cesium-1.72 learning (eagle eye map of the earth)
5G商用三年,未来创新何去何从?