1from heapq import heappop, heappush2from collections import defaultdict345class Solution:6def secondMinimum(7self, n: int, edges: List[List[int]], time: int, change: int8) -> int:9G = defaultdict(list)10dist = defaultdict(set)11for v, w in edges:12G[v].append(w)13G[w].append(v)14h = [(0, 1)]15res = []16while h:17d, v = heappop(h)18if len(dist[v]) > 1:19continue20if d in dist[v]:21continue22dist[v].add(d)23q, r = divmod(d, change)24if q % 2 == 1:25d += change - r26for w in G[v]:27if w == n:28if res:29if d + time not in res:30return d + time31else:32res.append(d + time)33if len(dist[w]) < 2:34heappush(h, (d + time, w))