結果

問題 No.160 最短経路のうち辞書順最小
ユーザー qibqib
提出日時 2022-12-14 18:47:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 100 ms / 5,000 ms
コード長 799 bytes
コンパイル時間 336 ms
コンパイル使用メモリ 82,500 KB
実行使用メモリ 77,620 KB
最終ジャッジ日時 2024-04-25 22:27:12
合計ジャッジ時間 3,509 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,604 KB
testcase_01 AC 36 ms
54,272 KB
testcase_02 AC 35 ms
53,796 KB
testcase_03 AC 35 ms
53,512 KB
testcase_04 AC 79 ms
76,972 KB
testcase_05 AC 87 ms
77,400 KB
testcase_06 AC 100 ms
77,620 KB
testcase_07 AC 66 ms
72,700 KB
testcase_08 AC 67 ms
73,404 KB
testcase_09 AC 71 ms
73,988 KB
testcase_10 AC 65 ms
74,080 KB
testcase_11 AC 68 ms
73,784 KB
testcase_12 AC 73 ms
74,944 KB
testcase_13 AC 67 ms
74,196 KB
testcase_14 AC 63 ms
72,172 KB
testcase_15 AC 64 ms
72,676 KB
testcase_16 AC 70 ms
73,652 KB
testcase_17 AC 69 ms
73,480 KB
testcase_18 AC 69 ms
72,692 KB
testcase_19 AC 70 ms
74,344 KB
testcase_20 AC 70 ms
75,288 KB
testcase_21 AC 66 ms
72,144 KB
testcase_22 AC 66 ms
71,848 KB
testcase_23 AC 69 ms
74,064 KB
testcase_24 AC 72 ms
75,020 KB
testcase_25 AC 66 ms
72,680 KB
testcase_26 AC 63 ms
73,104 KB
testcase_27 AC 42 ms
57,124 KB
testcase_28 AC 83 ms
77,156 KB
testcase_29 AC 38 ms
54,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

INF = 1 << 60

n, m, src, dst = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(m):
  a, b, c = map(int, input().split())
  g[a].append((b, c))
  g[b].append((a, c))

for u in range(n):
  g[u].sort(key=lambda x: x[0])

dist = [INF for _ in range(n)]
dist[dst] = 0
hp = [(dist[dst], dst)]
par = [None for _ in range(n)]
while len(hp) > 0:
  cd, cur = heapq.heappop(hp)
  if cd > dist[cur]:
    continue
  for nxt, c in g[cur]:
    if dist[cur] + c < dist[nxt]:
      par[nxt] = cur
      dist[nxt] = dist[cur] + c
      heapq.heappush(hp, (dist[nxt], nxt))

cur = src
ans = []
while True:
  ans.append(str(cur))
  if cur == dst:
    break
  nxt = None
  for v, c in g[cur]:
    if dist[v] + c == dist[cur]:
      nxt = v
      break
  cur = nxt

print(' '.join(ans))
0