【缺迪杰斯特拉和SPFA] 文巾解题 787. K 站中转内最便宜的航班
生活随笔
收集整理的這篇文章主要介紹了
【缺迪杰斯特拉和SPFA] 文巾解题 787. K 站中转内最便宜的航班
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 題目描述
2 解題思路?
2.1 動態規劃
我們記dp[i][k]表示最后一個點是k,且從src已經經過了step條邊時候的距離。
那么我們最終的目標是要找到最小的dp[i][dst](i∈[1,k+1])
每一次的轉移方程為 dp[i][dst]=min(dp[i-1][src]+len([src,dst])
于是我們使用動態規劃,有:
class Solution:def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:INF = 10 **9dp = [[INF for _ in range(n)] for _ in range(k + 2)]dp[0][src] = 0 #所有的初始值都是無窮大,除了起始點src(因為距離src0條邊距離的只有自己)for step in range(1, k + 2):for x, y, cost in flights:dp[step][y] = min(dp[step][y], dp[step - 1][x] + cost)#每一個step相當于更新 距離src更遠一階的鄰居的dp信息res = INFfor step in range(1, k + 2):res = min(res, dp[step][dst])#找終點中最小的dp值if(res==INF):return -1else:return res?2.2 動態規劃進階版
我們在看一遍2.1,不難發現動態規劃中的dp[i][...]只有在更新dp[i+1][....]的時候會用到,因此,我們只需要兩個一維的dp數組就可以完成2.1的操作
class Solution:def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:INF = 10 **9res=INFdp = [INF for _ in range(n)] dp[src] = 0 #相同的賦值方式for step in range(1, k + 2):dp2 = [INF for _ in range(n)] for x, y, cost in flights:dp2[y] = min(dp2[y], dp[x] + cost) #在這里dp2相當于當前step的dp,dp相當于上一個step的dpdp=dp2res=min(res,dp[dst]) #這一個step對應的dp[dst]值是否可以更新結果值if(res==INF):return -1else:return res?
2.3 BFS?
????????BFS可以做,就是需要在很多地方進行剪枝
????????設立一個隊列,里面的每一個元素是一個三元組:當前起始點,還剩下幾條邊可以走,從最初起始點到當前起始點之間的距離。
? ? ? ? 每一次從隊列中彈出一個元素(origin,num,dis),先判斷還能不能繼續去下一個節點dst,如果能得話,將(dst,num-1,dis+len([origin,dst]))入隊列;如果下一個節點就是終點的話,就判斷是否要更新結果那個最小值
class Solution:def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:dit=dict()for i in range(n):dit[i]=[]for i in flights:tmp=i[0]dit[i[0]].append([i[1],i[2]]) #建立一個字典,字典的key是起始點,value是相鄰點和起點終點之間連邊的長度lst=[[src,k+1,0]]m=10000000dist=[m for _ in range(n)]#dist表明從最初的起點到某一個點之間的最短距離,可以用于剪枝while(lst):a=lst.pop(0)origin=a[0]num=a[1]dis=a[2]x=num-1if(x<0):continue #已經沒有剩余邊了,剪枝if(dis>dist[origin]):continue #從最初的原點到當前點的距離 比目前從最初的原點到當前點的最短距離長,剪枝dist[origin]=dis #更新從最初的原點到當前點的距離for i in dit[origin]:destination=i[0]length=i[1]long=dis+lengthif(destination==dst):m=min(m,long) #到達終點,判斷要不要更新melif(long>m):passelse:lst.append([destination,x,long])if(m==10000000):m=-1return(m)?
?
總結
以上是生活随笔為你收集整理的【缺迪杰斯特拉和SPFA] 文巾解题 787. K 站中转内最便宜的航班的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文巾解题 1646. 获取生成数组中的最
- 下一篇: 论文笔记:Stochastic Weig