Advertisement
Egor_1425

Untitled

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