Skip to content

Instantly share code, notes, and snippets.

@ysinjab
Created September 29, 2022 16:23
Show Gist options
  • Save ysinjab/df97b63e92987294adb433ddd64f8cdb to your computer and use it in GitHub Desktop.
Save ysinjab/df97b63e92987294adb433ddd64f8cdb to your computer and use it in GitHub Desktop.
787. Cheapest Flights Within K Stops
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