結果

問題 No.2565 はじめてのおつかい
ユーザー nikoro256nikoro256
提出日時 2023-12-08 18:54:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 628 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,992 KB
最終ジャッジ日時 2023-12-08 18:54:16
合計ジャッジ時間 12,346 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,608 KB
testcase_01 AC 40 ms
55,608 KB
testcase_02 AC 39 ms
55,608 KB
testcase_03 AC 202 ms
89,988 KB
testcase_04 AC 155 ms
89,992 KB
testcase_05 AC 39 ms
55,608 KB
testcase_06 AC 171 ms
80,672 KB
testcase_07 AC 123 ms
77,836 KB
testcase_08 AC 139 ms
79,264 KB
testcase_09 AC 156 ms
79,112 KB
testcase_10 AC 148 ms
80,044 KB
testcase_11 AC 226 ms
84,972 KB
testcase_12 AC 108 ms
77,196 KB
testcase_13 AC 148 ms
79,368 KB
testcase_14 AC 190 ms
82,368 KB
testcase_15 AC 183 ms
81,664 KB
testcase_16 AC 147 ms
85,448 KB
testcase_17 AC 84 ms
77,064 KB
testcase_18 AC 95 ms
79,036 KB
testcase_19 AC 218 ms
83,092 KB
testcase_20 AC 178 ms
82,684 KB
testcase_21 AC 163 ms
82,308 KB
testcase_22 AC 126 ms
80,276 KB
testcase_23 AC 140 ms
80,264 KB
testcase_24 AC 86 ms
77,068 KB
testcase_25 AC 194 ms
83,088 KB
testcase_26 AC 146 ms
80,228 KB
testcase_27 AC 153 ms
82,872 KB
testcase_28 AC 197 ms
82,816 KB
testcase_29 AC 215 ms
84,900 KB
testcase_30 AC 170 ms
83,436 KB
testcase_31 AC 206 ms
82,960 KB
testcase_32 AC 136 ms
79,984 KB
testcase_33 AC 189 ms
83,668 KB
testcase_34 AC 197 ms
82,504 KB
testcase_35 AC 165 ms
84,432 KB
testcase_36 AC 206 ms
83,664 KB
testcase_37 AC 134 ms
80,124 KB
testcase_38 AC 176 ms
82,380 KB
testcase_39 AC 192 ms
81,804 KB
testcase_40 AC 179 ms
84,636 KB
testcase_41 AC 204 ms
85,468 KB
testcase_42 AC 136 ms
79,568 KB
testcase_43 AC 160 ms
81,332 KB
testcase_44 AC 138 ms
79,132 KB
testcase_45 AC 104 ms
77,448 KB
testcase_46 AC 196 ms
81,676 KB
testcase_47 AC 138 ms
78,860 KB
testcase_48 AC 145 ms
79,888 KB
testcase_49 AC 105 ms
77,324 KB
testcase_50 AC 146 ms
78,476 KB
testcase_51 AC 41 ms
55,608 KB
testcase_52 AC 120 ms
76,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
def bfs(s):
    dq=deque()
    dq.append([s,0])
    high=[10**9 for _ in range(N)]
    high[s]=0
    while len(dq)!=0:
        p,h=dq.popleft()
        for e in edge[p]:
            if  high[e]==10**9:
                dq.append([e,h+1])
                high[e]=h+1
    return high

N,M=map(int,input().split())
edge=[[] for _ in range(N)]
for i in range(M):
    u,v=map(int,input().split())
    u-=1
    v-=1
    edge[u].append(v)
dist0=bfs(0)
distn_=bfs(N-2)
distn=bfs(N-1)
ans=min(dist0[N-2]+distn_[N-1]+distn[0],dist0[N-1]+distn[N-2]+distn_[0])
if ans>10**9:
    print(-1)
else:
    print(ans)
0