rishiilluri

Untitled

Nov 11th, 2022
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. from queue import PriorityQueue
  2. def get_shortest_path_cost(A, s, t):
  3.     visit = [0 for i in A]
  4.     q = PriorityQueue()
  5.     q.put((0, s, str(s)))
  6.     visit[s] = 1
  7.    
  8.     while not q.empty():
  9.         x = q.get()
  10.         visit[x[1]] = 1
  11.         if x[1] == t:
  12.             return x[0], x[2]
  13.         for i in range(len(A[x[1]])):
  14.             if visit[i] == 0 and A[x[1]][i]!=0:
  15.                 q.put((x[0]+A[x[1]][i], i , x[2]+" "+str(i)))
  16.        
  17.        
  18.    
  19.  
  20. 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