Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def best_time(flight_info, current_city, destination_city):
- """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. """
- lines = flight_info.splitlines()
- header = lines[0]
- q = int(header.split()[1])
- mat = [[float('inf') for i in range(q)] for i in range(q)]
- is_directed = header[0] == 'D'
- for item in lines[1:]:
- numbers = [int(i) for i in item.split()]
- s = numbers[0]
- e = numbers[1]
- w = numbers[2]
- mat[s][e] = w
- if not is_directed:
- mat[e][s] = w
- for i in range(q):
- mat[i][i] = 0
- if current_city == destination_city:
- return 0
- else:
- for k in range(q):
- for i in range(q):
- for j in range(q):
- if mat[i][j] > mat[i][k] + mat[k][j]:
- mat[i][j] = mat[i][k] + mat[k][j]
- return mat[current_city][destination_city]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement