Created
September 29, 2022 16:23
-
-
Save ysinjab/df97b63e92987294adb433ddd64f8cdb to your computer and use it in GitHub Desktop.
787. Cheapest Flights Within K Stops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def findCheapestPrice(self, n, flights, src, dst, k): | |
# bellman-ford | |
p = [float('inf') for _ in range(n)] | |
c = [float('inf') for _ in range(n)] | |
p[src] = 0 | |
for i in range(k+1): | |
for u, v, w in flights: | |
c[v] = min(p[u]+w, c[v]) | |
p = copy.copy(c) | |
return -1 if c[dst] == float('inf') else c[dst] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment