Advertisement
Egor_1425

Untitled

Apr 2nd, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. def green(dist, color):
  2.     green_ = -1
  3.     minimum = 1000
  4.     for i, t in enumerate(dist):
  5.         if t < minimum and i not in color:
  6.             minimum = t
  7.             green_ = i
  8.     return green_
  9.  
  10. n, s, f = map(int, input().split())
  11. graph = [0] * n
  12. for i in range(n):
  13.     graph[i] = list(map(int, input().split()))
  14.  
  15. v = s - 1
  16. dist = [1000] * n
  17. color = {v}
  18. dist[v] = 0
  19.  
  20. while v != -1:
  21.     for j, dw in enumerate(graph[v]):
  22.         if dw > 0 and j not in color and dist[v] + dw < dist[j]:
  23.             dist[j] = dist[v] + dw
  24.     v = green(dist, color)
  25.     color.add(v)
  26.  
  27. if dist[f-1] == 1000:
  28.     print(-1)
  29. else:
  30.     print(dist[f-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement