結果

問題 No.1565 Union
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-06-26 13:17:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 847 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 101,340 KB
最終ジャッジ日時 2023-12-18 22:33:54
合計ジャッジ時間 5,472 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,612 KB
testcase_01 AC 37 ms
55,612 KB
testcase_02 AC 41 ms
55,612 KB
testcase_03 AC 38 ms
55,612 KB
testcase_04 AC 37 ms
55,612 KB
testcase_05 AC 36 ms
55,612 KB
testcase_06 AC 37 ms
55,612 KB
testcase_07 AC 37 ms
55,612 KB
testcase_08 AC 37 ms
55,612 KB
testcase_09 AC 37 ms
55,612 KB
testcase_10 AC 100 ms
80,208 KB
testcase_11 AC 159 ms
93,528 KB
testcase_12 AC 142 ms
85,020 KB
testcase_13 AC 87 ms
79,980 KB
testcase_14 AC 179 ms
90,612 KB
testcase_15 AC 263 ms
100,064 KB
testcase_16 AC 204 ms
99,808 KB
testcase_17 AC 272 ms
100,064 KB
testcase_18 AC 265 ms
100,064 KB
testcase_19 AC 270 ms
100,064 KB
testcase_20 AC 144 ms
101,340 KB
testcase_21 AC 148 ms
101,340 KB
testcase_22 AC 146 ms
101,340 KB
testcase_23 AC 150 ms
101,340 KB
testcase_24 AC 147 ms
101,340 KB
testcase_25 AC 161 ms
101,340 KB
testcase_26 AC 157 ms
101,340 KB
testcase_27 AC 162 ms
101,340 KB
testcase_28 AC 167 ms
101,340 KB
testcase_29 AC 160 ms
101,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

https://yukicoder.me/problems/no/1565


"""

from sys import stdin
#重みのないグラフでの最短経路問題
#隣接リストと始点を与えると始点からの距離のリスト & 親のリストを返す
from collections import deque
def NC_Dij(lis,start):

    ret = [float("inf")] * len(lis)
    ret[start] = 0
    
    q = deque([start])

    while len(q) > 0:
        now = q.popleft()

        for nex in lis[now]:

            if ret[nex] > ret[now] + 1:
                ret[nex] = ret[now] + 1
                q.append(nex)

    return ret
N,M = map(int,stdin.readline().split())

lis = [ [] for i in range(N) ]

for i in range(M):

    a,b = map(int,stdin.readline().split())
    a -= 1
    b -= 1
    lis[a].append(b)
    lis[b].append(a)

dlis = NC_Dij(lis,0)

ans = dlis[-1]

print (ans if ans!=float("inf") else -1)
0