Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- graph = {}
- coords = {}
- n = int(input())
- ancestors = {}
- MAXI = float(10 ** 9)
- dist = {}
- used = {}
- def restore_path(v, to, ancestors):
- start = to
- path = []
- while start != -1:
- path.append(start)
- start = ancestors[start]
- path = path[::-1]
- return path[:]
- def get_len(first, second, coords):
- return math.sqrt((coords[first][0] - coords[second][0]) ** 2 + (coords[first][1] - coords[second][1]) ** 2)
- def dijkstra(v, to, graph, dist, used, ancestors):
- dist[v] = 0
- for i in range(n):
- mini, ind = MAXI, None
- for j in dist:
- if mini > dist[j] and (not used[j]):
- mini = dist[j]
- ind = j
- if ind is None or mini == MAXI:
- break
- used[ind] = True
- for city in graph[ind]:
- len_ = get_len(city, ind, coords)
- if dist[city] > dist[ind] + len_:
- dist[city] = dist[ind] + len_
- ancestors[city] = ind
- for i in range(n):
- input_parts = input().split(' ')
- city = input_parts[0]
- coord_x = int(input_parts[1])
- coord_y = int(input_parts[2])
- graph[city] = []
- dist[city] = MAXI
- used[city] = False
- ancestors[city] = -1
- coords[city] = [coord_x, coord_y]
- for j in range(3, len(input_parts)):
- if len(input_parts[j]) > 0:
- graph[city].append(input_parts[j])
- v, to = input().split()
- dijkstra(v, to, graph, dist, used, ancestors)
- if dist[to] == MAXI:
- print("Path:\nNo way")
- else:
- print("Path is not greater than", int(dist[to] + 1))
- print("Path:")
- path = restore_path(v, to, ancestors)
- for x in path:
- print(x, end = ' ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement