File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ #
2+ # https://www.acmicpc.net/problem/24042
3+
4+ """
5+ 4 5
6+ 1 2
7+ 3 4
8+ 1 3
9+ 4 1
10+ 2 3
11+ """
12+
13+ input = __import__ ("sys" ).stdin .readline
14+
15+ N ,M = map (int , input ().split ())
16+ graph = [[] for _ in range (N + 1 )]
17+ for i in range (M ):
18+ a ,b = map (int , input ().split ())
19+ graph [a ].append ((b ,i ))
20+ graph [b ].append ((a ,i ))
21+
22+ INF = int (1e11 )
23+
24+ def dijkstra ():
25+ import heapq
26+
27+ dist = [INF ]* (N + 1 )
28+ heap = [(0 , 1 )]
29+ dist [1 ] = 0
30+
31+ while heap :
32+ cost , node = heapq .heappop (heap )
33+
34+ if dist [node ] < cost :
35+ continue
36+
37+ timing = cost % M
38+ for nxt_node , nxt_timing in graph [node ]:
39+ nxt_cost = dist [node ]+ 1
40+ if nxt_timing >= timing :
41+ nxt_cost += nxt_timing - timing
42+ else :
43+ nxt_cost += M + nxt_timing - timing
44+
45+ if nxt_cost >= dist [nxt_node ]:
46+ continue
47+
48+ dist [nxt_node ] = nxt_cost
49+ heapq .heappush (heap , (nxt_cost , nxt_node ))
50+
51+ print (dist [N ])
52+
53+ dijkstra ()
You can’t perform that action at this time.
0 commit comments