当前位置:网站首页>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
边栏推荐
- 编译生成busybox文件系统
- Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution
- 分享 5 大常用的特征选择方法,机器学习入门必看!!!
- 【剑指Offer】52. 两个链表的第一个公共节点
- IEEE TBD SCI影响因子提升至4.271,位列Q1区!
- 美国穆格moog伺服阀D661-4577C
- Exploration and practice of "flow batch integration" in JD
- splitting. JS password display hidden JS effect
- 自旋锁探秘
- k线图精解与实战应用技巧(见位进场)
猜你喜欢
随机推荐
5g has been in business for three years. Where will innovation go in the future?
Rexroth hydraulic control check valve z2s10-1-3x/
理解现货白银走势的关键
生成对抗网络,从DCGAN到StyleGAN、pixel2pixel,人脸生成和图像翻译。
[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
If your MES is not upgraded, it will be eliminated
3D图表有效提升数据大屏档次
Send the injured baby for emergency medical treatment. Didi's driver ran five red lights in a row
Cesium-1.72 learning (model attitude control)
[Netease Yunxin] playback demo build: unable to convert parameter 1 from "asyncmodalrunner *" to "std:: nullptr\u T"**
. Net ORM framework hisql practice - Chapter 1 - integrating hisql
Implementation of graduation project management system based on SSM
Parker Parker sensor p8s-grflx
k线图快速入门必读
splitting. JS password display hidden JS effect
万卷书 - 欧洲的门户 [The Gates of Europe]
Network: principle and practice of server network card group technology
【剑指Offer】53 - I. 在排序数组中查找数字 I
leetcode:787. K 站中转内最便宜的航班【k步最短路 + dfs记忆化 + defaultdict(dict)】
MOOG servo valve d661-4577c



![[Architecture] 1366- how to draw an excellent architecture diagram](/img/98/5dc29e08e91e751f67d910fadc6430.jpg)





