Часть тестов проходит успешно, часть тестов – падает. Тестовые данные скрыты, я не понимаю, что тут может быть не так и что с этим делать
HEEEEELP
from collections import defaultdict
number_of_elements, number_of_roads = map(int, input().split())
input_list = [tuple(map(int, input().split())) for _ in range(number_of_roads)]
# Чтение входных данных
source = 1
target = number_of_elements
graph = defaultdict(list)
for a, b, cost in input_list:
graph[a] += [(cost, b)]
# Инициализация переменных
nodes_to_visit = []
nodes_to_visit.append((0, source))
visited = set()
min_dist = {i: float('inf') for i in range(1, number_of_elements + 1)}
min_dist[source] = 0
min_path = {i: float('inf') for i in range(1, number_of_elements + 1)}
min_path[source] = -1 # Начальная вершина не имеет предыдущей вершины на пути
# Выполнение алгоритма Дейкстры
while len(nodes_to_visit):
cost, node = min(nodes_to_visit)
nodes_to_visit.remove((cost, node))
if node == target:
break
if node in visited:
continue
visited.add(node)
for n_cost, n_node in graph[node]:
if cost + n_cost < min_dist[n_node] and n_node not in visited:
min_dist[n_node] = cost + n_cost
min_path[n_node] = node # Обновляем ближайшую вершину для текущей вершины
nodes_to_visit.append((cost + n_cost, n_node))
# Восстановление пути
path = []
current_node = target
while current_node != source:
previous = min_path.get(current_node)
if previous is None:
print(-1)
exit()
path.append(current_node)
current_node = previous
path.append(source)
# Вывод количества поселений на кратчайшем пути
path_length = len(path)
print(path_length)
print(' '.join(map(str, path[::-1])))
Код уже состоит из чужого кода, но все равно не проходит тесты
У других пользователей все ок, я прочитала и просмотрела уже все, что можно
New contributor
KarlLa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.