結果

問題 No.1565 Union
ユーザー AEnAEn
提出日時 2022-09-03 16:39:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 855 ms / 2,000 ms
コード長 764 bytes
コンパイル時間 442 ms
コンパイル使用メモリ 82,468 KB
実行使用メモリ 114,936 KB
最終ジャッジ日時 2024-05-01 18:26:02
合計ジャッジ時間 12,481 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,012 KB
testcase_01 AC 40 ms
52,560 KB
testcase_02 AC 41 ms
52,824 KB
testcase_03 AC 40 ms
52,952 KB
testcase_04 AC 41 ms
54,172 KB
testcase_05 AC 42 ms
53,496 KB
testcase_06 AC 42 ms
53,432 KB
testcase_07 AC 42 ms
53,008 KB
testcase_08 AC 41 ms
52,732 KB
testcase_09 AC 41 ms
53,540 KB
testcase_10 AC 248 ms
89,012 KB
testcase_11 AC 434 ms
101,988 KB
testcase_12 AC 430 ms
96,232 KB
testcase_13 AC 229 ms
85,240 KB
testcase_14 AC 541 ms
101,908 KB
testcase_15 AC 827 ms
114,408 KB
testcase_16 AC 445 ms
111,188 KB
testcase_17 AC 855 ms
114,936 KB
testcase_18 AC 822 ms
114,932 KB
testcase_19 AC 816 ms
114,184 KB
testcase_20 AC 279 ms
112,588 KB
testcase_21 AC 292 ms
112,548 KB
testcase_22 AC 286 ms
112,552 KB
testcase_23 AC 297 ms
112,624 KB
testcase_24 AC 294 ms
112,828 KB
testcase_25 AC 382 ms
112,556 KB
testcase_26 AC 374 ms
112,820 KB
testcase_27 AC 376 ms
112,604 KB
testcase_28 AC 366 ms
112,348 KB
testcase_29 AC 365 ms
113,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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