Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. def best_time(flight_info, current_city, destination_city):
  2. """that takes the following information and returns the shortest time possible to get back home. If there is no way to get home, the function returns infinity. """
  3.  
  4. lines = flight_info.splitlines()
  5. header = lines[0]
  6.  
  7. q = int(header.split()[1])
  8.  
  9.  
  10. mat = [[float('inf') for i in range(q)] for i in range(q)]
  11. is_directed = header[0] == 'D'
  12.  
  13. for item in lines[1:]:
  14. numbers = [int(i) for i in item.split()]
  15. s = numbers[0]
  16. e = numbers[1]
  17. w = numbers[2]
  18. mat[s][e] = w
  19. if not is_directed:
  20. mat[e][s] = w
  21. for i in range(q):
  22. mat[i][i] = 0
  23. if current_city == destination_city:
  24. return 0
  25. else:
  26. for k in range(q):
  27. for i in range(q):
  28. for j in range(q):
  29. if mat[i][j] > mat[i][k] + mat[k][j]:
  30. mat[i][j] = mat[i][k] + mat[k][j]
  31. return mat[current_city][destination_city]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement