結果

問題 No.1565 Union
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-01-22 19:15:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 530 ms / 2,000 ms
コード長 769 bytes
コンパイル時間 330 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 113,492 KB
最終ジャッジ日時 2024-05-05 02:18:43
合計ジャッジ時間 7,949 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
54,912 KB
testcase_01 AC 43 ms
55,296 KB
testcase_02 AC 50 ms
55,552 KB
testcase_03 AC 50 ms
55,296 KB
testcase_04 AC 49 ms
55,040 KB
testcase_05 AC 51 ms
55,424 KB
testcase_06 AC 44 ms
55,680 KB
testcase_07 AC 45 ms
55,680 KB
testcase_08 AC 49 ms
55,424 KB
testcase_09 AC 46 ms
55,808 KB
testcase_10 AC 177 ms
88,576 KB
testcase_11 AC 275 ms
99,900 KB
testcase_12 AC 246 ms
96,000 KB
testcase_13 AC 151 ms
84,352 KB
testcase_14 AC 349 ms
101,420 KB
testcase_15 AC 518 ms
113,452 KB
testcase_16 AC 268 ms
106,496 KB
testcase_17 AC 530 ms
113,332 KB
testcase_18 AC 529 ms
113,492 KB
testcase_19 AC 529 ms
113,220 KB
testcase_20 AC 182 ms
109,824 KB
testcase_21 AC 190 ms
109,824 KB
testcase_22 AC 183 ms
110,188 KB
testcase_23 AC 189 ms
110,464 KB
testcase_24 AC 204 ms
110,464 KB
testcase_25 AC 257 ms
110,976 KB
testcase_26 AC 247 ms
110,336 KB
testcase_27 AC 247 ms
110,816 KB
testcase_28 AC 253 ms
110,848 KB
testcase_29 AC 254 ms
110,208 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