結果

問題 No.1565 Union
ユーザー ThetaTheta
提出日時 2024-04-16 11:50:25
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 907 bytes
コンパイル時間 378 ms
コンパイル使用メモリ 82,100 KB
実行使用メモリ 166,096 KB
最終ジャッジ日時 2024-04-16 11:50:37
合計ジャッジ時間 11,725 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,400 KB
testcase_01 AC 41 ms
54,400 KB
testcase_02 AC 40 ms
54,528 KB
testcase_03 AC 40 ms
54,912 KB
testcase_04 AC 39 ms
54,784 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 41 ms
54,400 KB
testcase_09 WA -
testcase_10 AC 459 ms
93,440 KB
testcase_11 AC 367 ms
121,748 KB
testcase_12 AC 443 ms
100,828 KB
testcase_13 AC 190 ms
88,644 KB
testcase_14 AC 492 ms
115,236 KB
testcase_15 AC 728 ms
148,844 KB
testcase_16 AC 488 ms
128,856 KB
testcase_17 WA -
testcase_18 AC 678 ms
148,360 KB
testcase_19 WA -
testcase_20 AC 299 ms
163,780 KB
testcase_21 AC 320 ms
164,876 KB
testcase_22 AC 299 ms
163,520 KB
testcase_23 AC 331 ms
164,280 KB
testcase_24 AC 309 ms
164,504 KB
testcase_25 AC 338 ms
165,928 KB
testcase_26 AC 358 ms
165,768 KB
testcase_27 AC 342 ms
166,096 KB
testcase_28 AC 341 ms
164,708 KB
testcase_29 AC 340 ms
165,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
from heapq import heappop
from math import inf, isinf


def bfs_dist(graph: list[set[int]], start: int, goal: int) -> int | float:
    distance = [inf for _ in graph]
    distance[start] = 0
    queue = deque([start])
    visited = set()
    while queue:
        current = queue.popleft()
        visited.add(current)
        for dest in graph[current]:
            if dest in visited:
                continue
            queue.append(dest)
            distance[dest] = distance[current] + 1

    return distance[goal]


def main():
    N, M = map(int, input().split())
    graph = [set() for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        graph[a - 1].add(b - 1)
        graph[b - 1].add(a - 1)

    if isinf((dist := bfs_dist(graph, 0, N - 1))):
        print(-1)
    else:
        print(dist)


if __name__ == "__main__":
    main()
0