結果

問題 No.1565 Union
ユーザー ああいいああいい
提出日時 2021-12-08 22:58:29
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,338 ms / 2,000 ms
コード長 569 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 54,784 KB
最終ジャッジ日時 2024-05-02 02:14:47
合計ジャッジ時間 21,735 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,752 KB
testcase_01 AC 28 ms
10,752 KB
testcase_02 AC 27 ms
10,752 KB
testcase_03 AC 33 ms
10,752 KB
testcase_04 AC 29 ms
10,752 KB
testcase_05 AC 29 ms
10,752 KB
testcase_06 AC 29 ms
10,752 KB
testcase_07 AC 29 ms
10,752 KB
testcase_08 AC 27 ms
10,752 KB
testcase_09 AC 28 ms
10,752 KB
testcase_10 AC 446 ms
23,296 KB
testcase_11 AC 645 ms
37,632 KB
testcase_12 AC 695 ms
32,128 KB
testcase_13 AC 238 ms
19,200 KB
testcase_14 AC 824 ms
38,528 KB
testcase_15 AC 1,331 ms
50,176 KB
testcase_16 AC 833 ms
48,000 KB
testcase_17 AC 1,337 ms
50,048 KB
testcase_18 AC 1,315 ms
50,176 KB
testcase_19 AC 1,338 ms
50,176 KB
testcase_20 AC 930 ms
54,784 KB
testcase_21 AC 953 ms
54,784 KB
testcase_22 AC 935 ms
54,784 KB
testcase_23 AC 998 ms
54,784 KB
testcase_24 AC 978 ms
54,784 KB
testcase_25 AC 1,015 ms
54,784 KB
testcase_26 AC 1,022 ms
54,784 KB
testcase_27 AC 1,016 ms
54,784 KB
testcase_28 AC 1,032 ms
54,784 KB
testcase_29 AC 1,019 ms
54,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

N,M = map(int,input().split())
a = [0] * M
b = [0] * M
for i in range(M):
    a[i],b[i] = map(int,input().split())
v = [[] for _ in range(N+1)]
for i in range(M):
    v[a[i]].append(b[i])
    v[b[i]].append(a[i])
    
INF = 10 ** 6
dis = [INF] * (N+1)
dis[1] = 0
q = []
heapq.heapify(q)
heapq.heappush(q,(dis[1],1))
while len(q):
    d,s = heapq.heappop(q)
    if dis[s] < d:continue

    for i in v[s]:
        if dis[i] > dis[s] + 1:
            dis[i] = dis[s] + 1
            heapq.heappush(q,(dis[i],i))
if dis[N] == INF:print(-1)
else:print(dis[N])
0