結果

問題 No.1565 Union
ユーザー AEnAEn
提出日時 2022-09-03 16:39:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 654 ms / 2,000 ms
コード長 764 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 115,444 KB
最終ジャッジ日時 2024-05-18 12:42:58
合計ジャッジ時間 9,716 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,480 KB
testcase_01 AC 40 ms
52,608 KB
testcase_02 AC 37 ms
52,736 KB
testcase_03 AC 35 ms
52,736 KB
testcase_04 AC 38 ms
52,768 KB
testcase_05 AC 36 ms
52,864 KB
testcase_06 AC 36 ms
52,992 KB
testcase_07 AC 37 ms
53,120 KB
testcase_08 AC 36 ms
52,736 KB
testcase_09 AC 38 ms
52,864 KB
testcase_10 AC 207 ms
89,344 KB
testcase_11 AC 346 ms
102,848 KB
testcase_12 AC 327 ms
96,260 KB
testcase_13 AC 186 ms
85,376 KB
testcase_14 AC 405 ms
102,672 KB
testcase_15 AC 651 ms
114,660 KB
testcase_16 AC 348 ms
111,600 KB
testcase_17 AC 654 ms
115,444 KB
testcase_18 AC 630 ms
115,068 KB
testcase_19 AC 617 ms
114,816 KB
testcase_20 AC 244 ms
112,672 KB
testcase_21 AC 257 ms
113,124 KB
testcase_22 AC 242 ms
113,000 KB
testcase_23 AC 252 ms
112,860 KB
testcase_24 AC 260 ms
112,572 KB
testcase_25 AC 330 ms
113,176 KB
testcase_26 AC 316 ms
113,048 KB
testcase_27 AC 324 ms
113,244 KB
testcase_28 AC 324 ms
113,028 KB
testcase_29 AC 320 ms
112,996 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