結果
問題 |
No.1565 Union
|
ユーザー |
![]() |
提出日時 | 2022-09-03 16:39:47 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 772 ms / 2,000 ms |
コード長 | 764 bytes |
コンパイル時間 | 335 ms |
コンパイル使用メモリ | 82,396 KB |
実行使用メモリ | 115,192 KB |
最終ジャッジ日時 | 2024-12-20 16:21:36 |
合計ジャッジ時間 | 11,569 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
from heapq import heappop, heappush INF = float('inf') def dijkstra(s, n): dist = [INF] * n hq = [(0, s)] # (distance, node) dist[s] = 0 seen = [False] * n # ノードが確定済みかどうか while hq: dis, v = heappop(hq) if dist[v] < dis: continue seen[v] = True for to, cost in G[v]: if seen[to] == False and dist[v] + cost < dist[to]: dist[to] = dist[v] + cost heappush(hq, (dist[to], to)) return dist N, M = map(int, input().split()) G = [list() for _ in range(N)] for i in range(M): a, b = map(int, input().split()) a-=1;b-=1 G[a].append((b,1)) G[b].append((a,1)) d = dijkstra(0, N) print(-1) if d[-1]==INF else print(d[-1])