-
Notifications
You must be signed in to change notification settings - Fork 0
/
20C.py
108 lines (93 loc) · 2.97 KB
/
20C.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from heapq import heapify, heappush, heappop
from dataclasses import dataclass, field
from typing import Any
from itertools import count
@dataclass(order=True)
class Node:
priority: Any = field(compare=True)
task: Any = field(compare=False)
class PriorityHeap:
def __init__(self, lst):
self.counter = count()
self.size = 0
self.entry_finder = {}
self.heap = list()
for task, pri in lst.items():
node = self.create_node(task, pri)
self.entry_finder[task] = node
self.heap.append(node)
heapify(self.heap)
self.size = len(self.heap)
def create_node(self, task, priority):
return Node((priority, next(self.counter)), task)
def add_task(self, task, priority):
if task in self.entry_finder:
self.remove_task(task)
node = self.create_node(task, priority)
self.entry_finder[task] = node
heappush(self.heap, node)
self.size += 1
def remove_task(self, task):
entry = self.entry_finder.pop(task)
entry.task = None
self.size -= 1
def pop_task(self):
while self.size > 0:
node = heappop(self.heap)
if node.task is not None:
del self.entry_finder[node.task]
self.size -= 1
return node.task
self.heap = list()
raise KeyError('pop from an empty priority queue')
def __len__(self):
return self.size
#def dijkstra(graph, vertices, source, dest):
# distance = {source: 0}
# ph = PriorityHeap(distance)
# parent = {}
# while len(ph) != 0:
# vertex = ph.pop_task()
# for v, w in graph[vertex]:
# dist = distance.get(v, float('inf'))
# if dist > distance[vertex] + w:
# distance[v] = distance[vertex] + w
# ph.add_task(v, distance[v])
# parent[v] = vertex
# if vertex == dest:
# return distance, parent
# return distance, parent
def dijkstra(graph, vertices, source, dest):
distance = {source: 0}
ph = PriorityHeap(distance)
parent = dict()
while len(ph) != 0:
vertex = ph.pop_task()
for v, w in graph[vertex]:
if distance.get(v, float('inf')) > distance[vertex] + w:
distance[v] = distance[vertex] + w
ph.add_task(v, distance[v])
parent[v] = vertex
if vertex == dest:
return distance, parent
return distance, parent
n, m = (int(x) for x in input().split())
vertices = range(1, n+1)
graph = {}
for v in vertices:
graph[v] = list()
for _ in range(m):
a, b, w = (int(x) for x in input().split())
graph[a].append((b, w))
graph[b].append((a, w))
distance, parent = dijkstra(graph, vertices, 1, n)
if n not in parent:
print(-1)
else:
ans = []
cur = n
while cur != 1:
ans.append(str(cur))
cur = parent[cur]
ans.append(str(1))
print(" ".join(reversed(ans)))