-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj-13549.py
43 lines (30 loc) · 851 Bytes
/
boj-13549.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
import heapq
def dijkstra(g: dict, s: int, t: int) -> int:
q = []
heapq.heappush(q, (0, s))
d = [float('inf')] * (len(g) + 1)
d[s] = 0
while q:
dist, u = heapq.heappop(q)
if dist > d[u]:
continue
if u == t:
return d[u]
for v, w in g[u]:
if d[v] > d[u] + w:
d[v] = d[u] + w
heapq.heappush(q, (d[v], v))
return -1
def main():
src, dst = map(int, input().split())
graph = {v: [] for v in range(100001)}
for v in range(100001):
if v > 0:
graph[v].append((v - 1, 1))
if v < 100000:
graph[v].append((v + 1, 1))
if v < 50001:
graph[v].append((v * 2, 0))
print(dijkstra(graph, src, dst))
if __name__ == "__main__":
main()