Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- INF = 1000000
- def green(dist, color):
- green_ = -1
- minimum = INF
- for i, t in enumerate(dist):
- if t < minimum and i not in color:
- minimum = t
- green_ = i
- return green_
- n = int(input())
- cost = list(map(int, input().split()))
- graph = [[0] * n for i in range(n)]
- m = int(input())
- for i in range(m):
- a1, a2 = map(int, input().split())
- graph[a1-1][a2-1] = cost[a1-1]
- graph[a2-1][a1-1] = cost[a2-1]
- f = n - 1
- v = 0
- dist = [INF] * n
- color = {v}
- dist[v] = 0
- while v != -1:
- for j, dw in enumerate(graph[v]):
- if j not in color and dist[v] + dw < dist[j] and dw > 0:
- dist[j] = dist[v] + dw
- v = green(dist, color)
- color.add(v)
- if dist[f] == INF:
- print(-1)
- else:
- print(dist[f])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement