結果
問題 |
No.1565 Union
|
ユーザー |
|
提出日時 | 2023-01-22 19:15:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 686 ms / 2,000 ms |
コード長 | 769 bytes |
コンパイル時間 | 2,126 ms |
コンパイル使用メモリ | 81,620 KB |
実行使用メモリ | 113,460 KB |
最終ジャッジ日時 | 2024-12-20 20:06:07 |
合計ジャッジ時間 | 10,176 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.readline N,M = map(int,input().split()) e = [[] for _ in range(N)] for _ in range(M): a,b = map(int,input().split()) a -= 1 b -= 1 e[a].append((b,1)) e[b].append((a,1)) INF = (1<<40) def dijkstra(s,e): N = len(e) dist = [INF]*N dist[s]=0 h = [] heappush(h,(0,s)) while h: nw,v = heappop(h) if dist[v]!=nw: continue for iv,ic in e[v]: nc = ic + nw if nc < dist[iv]: dist[iv] = nc heappush(h,(nc,iv)) return dist D = dijkstra(0,e) ans = D[N-1] if ans==INF: print(-1) else: print(ans)