Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from queue import PriorityQueue
- def get_shortest_path_cost(A, s, t):
- visit = [0 for i in A]
- q = PriorityQueue()
- q.put((0, s, str(s)))
- visit[s] = 1
- while not q.empty():
- x = q.get()
- visit[x[1]] = 1
- if x[1] == t:
- return x[0], x[2]
- for i in range(len(A[x[1]])):
- if visit[i] == 0 and A[x[1]][i]!=0:
- q.put((x[0]+A[x[1]][i], i , x[2]+" "+str(i)))
- get_shortest_path_cost([[0,2,3,0],[2,0,9,0],[3,9,0,6],[0,0,6,0]], 1, 3)
Advertisement
Add Comment
Please, Sign In to add comment