結果

問題 No.2565 はじめてのおつかい
ユーザー titiatitia
提出日時 2023-12-15 00:58:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 592 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,984 KB
最終ジャッジ日時 2023-12-15 00:58:59
合計ジャッジ時間 11,298 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,608 KB
testcase_01 AC 40 ms
55,608 KB
testcase_02 AC 41 ms
55,608 KB
testcase_03 AC 231 ms
89,984 KB
testcase_04 AC 159 ms
89,984 KB
testcase_05 AC 41 ms
55,608 KB
testcase_06 AC 216 ms
79,640 KB
testcase_07 AC 131 ms
77,832 KB
testcase_08 AC 171 ms
79,000 KB
testcase_09 AC 169 ms
79,104 KB
testcase_10 AC 153 ms
79,268 KB
testcase_11 AC 227 ms
84,080 KB
testcase_12 AC 109 ms
77,196 KB
testcase_13 AC 145 ms
78,336 KB
testcase_14 AC 190 ms
81,592 KB
testcase_15 AC 227 ms
81,400 KB
testcase_16 AC 155 ms
85,572 KB
testcase_17 AC 89 ms
77,064 KB
testcase_18 AC 91 ms
79,156 KB
testcase_19 AC 203 ms
82,956 KB
testcase_20 AC 183 ms
82,164 KB
testcase_21 AC 169 ms
82,172 KB
testcase_22 AC 141 ms
80,268 KB
testcase_23 AC 148 ms
80,128 KB
testcase_24 AC 89 ms
77,064 KB
testcase_25 AC 205 ms
83,080 KB
testcase_26 AC 148 ms
79,836 KB
testcase_27 AC 159 ms
82,736 KB
testcase_28 AC 204 ms
82,680 KB
testcase_29 AC 241 ms
84,764 KB
testcase_30 AC 173 ms
83,300 KB
testcase_31 AC 203 ms
82,440 KB
testcase_32 AC 145 ms
79,976 KB
testcase_33 AC 187 ms
83,148 KB
testcase_34 AC 211 ms
82,496 KB
testcase_35 AC 195 ms
84,552 KB
testcase_36 AC 215 ms
83,912 KB
testcase_37 AC 141 ms
80,116 KB
testcase_38 AC 193 ms
81,604 KB
testcase_39 AC 195 ms
81,028 KB
testcase_40 AC 191 ms
84,500 KB
testcase_41 AC 242 ms
85,460 KB
testcase_42 AC 150 ms
79,560 KB
testcase_43 AC 205 ms
81,324 KB
testcase_44 AC 148 ms
78,996 KB
testcase_45 AC 110 ms
77,440 KB
testcase_46 AC 213 ms
81,284 KB
testcase_47 AC 156 ms
78,728 KB
testcase_48 AC 160 ms
79,368 KB
testcase_49 AC 111 ms
77,196 KB
testcase_50 AC 161 ms
78,472 KB
testcase_51 AC 43 ms
55,608 KB
testcase_52 AC 126 ms
77,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N,M=map(int,input().split())
E=[[] for i in range(N+1)]

for i in range(M):
    x,y=map(int,input().split())
    E[x].append(y)

def distance_from(x):
    DIS=[1<<30]*(N+1)
    DIS[x]=0

    Q=deque([x])

    while Q:
        x=Q.popleft()

        for to in E[x]:
            if DIS[to]>DIS[x]+1:
                DIS[to]=DIS[x]+1
                Q.append(to)

    return DIS

ONE=distance_from(1)
DN=distance_from(N-1)
DN1=distance_from(N)

ANS=ONE[N]+DN1[N-1]+DN[1]
ANS2=ONE[N-1]+DN[N]+DN1[1]

LA=min(ANS,ANS2)

if LA>10**7:
    print(-1)
else:
    print(LA)
0