Обновить u10/najkratsia_cesta.py

This commit is contained in:
Arina Dohadailo 2026-11-27 00:57:21 +00:00
parent 7e1bc04e77
commit 2333c4eaec

View File

@ -27,3 +27,15 @@ def shortest_path(number_of_nodes, connections):
for neighbor, weight in graph[current]: for neighbor, weight in graph[current]:
new_distance = distances[current] + weight new_distance = distances[current] + weight
new_path = paths[current] + [neighbor] new_path = paths[current] + [neighbor]
if new_distance < distances[neighbor]:
distances[neighbor] = new_distance
paths[neighbor] = new_path
elif new_distance == distances[neighbor]:
if paths[neighbor] is None or new_path < paths[neighbor]:
paths[neighbor] = new_path
if paths[number_of_nodes - 1] is None:
return []
return paths[number_of_nodes - 1]