結果

問題 No.1565 Union
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-01-22 19:15:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 539 ms / 2,000 ms
コード長 769 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 82,524 KB
実行使用メモリ 113,848 KB
最終ジャッジ日時 2024-05-26 01:21:54
合計ジャッジ時間 8,160 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
56,376 KB
testcase_01 AC 42 ms
55,760 KB
testcase_02 AC 40 ms
56,668 KB
testcase_03 AC 40 ms
55,424 KB
testcase_04 AC 42 ms
56,152 KB
testcase_05 AC 40 ms
56,420 KB
testcase_06 AC 42 ms
56,288 KB
testcase_07 AC 41 ms
56,504 KB
testcase_08 AC 41 ms
56,684 KB
testcase_09 AC 41 ms
55,972 KB
testcase_10 AC 152 ms
88,532 KB
testcase_11 AC 280 ms
100,552 KB
testcase_12 AC 261 ms
96,480 KB
testcase_13 AC 151 ms
84,452 KB
testcase_14 AC 338 ms
101,460 KB
testcase_15 AC 525 ms
113,196 KB
testcase_16 AC 260 ms
106,384 KB
testcase_17 AC 539 ms
113,592 KB
testcase_18 AC 537 ms
113,360 KB
testcase_19 AC 532 ms
113,848 KB
testcase_20 AC 179 ms
110,048 KB
testcase_21 AC 186 ms
110,764 KB
testcase_22 AC 177 ms
110,492 KB
testcase_23 AC 192 ms
110,368 KB
testcase_24 AC 186 ms
110,508 KB
testcase_25 AC 258 ms
110,748 KB
testcase_26 AC 240 ms
110,428 KB
testcase_27 AC 247 ms
110,568 KB
testcase_28 AC 243 ms
111,032 KB
testcase_29 AC 234 ms
110,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0