-
Notifications
You must be signed in to change notification settings - Fork 0
/
santasspecialcottoncandies.py
50 lines (37 loc) · 1.12 KB
/
santasspecialcottoncandies.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
from collections import defaultdict
import math
import heapq as hq
entry = input()
first_line = list(map(int, entry.split()))
if len(first_line)!=2:
print('Error')
else:
[n,m] = first_line
class Graph():
def __init__(self):
self.graph = defaultdict(dict)
def add_edge(self, u,v,w):
self.graph[u-1][v-1]=w
def dijkstra(self):
dist = [-1]*n
dist[0] = 0
pqueue = []
hq.heappush(pqueue, (dist[0], 0))
hq.heapify(pqueue)
while(pqueue!=[]):
[_,u] = hq.heappop(pqueue)
for neighbor in self.graph[u].keys():
cost = max(dist[u], self.graph[u][neighbor])
if dist[neighbor]==-1 or cost < dist[neighbor]:
dist[neighbor] = cost
hq.heappush(pqueue, (dist[neighbor], neighbor))
print(str(dist[n-1])+"\n")
return dist[n-1]
G=Graph()
for i in range(m):
entry = input()
line = list(map(int, entry.split()))
[u,v,w]=line
G.add_edge(u,v,w)
G.add_edge(v,u,w)
G.dijkstra()