結果

問題 No.2565 はじめてのおつかい
ユーザー 👑 tipstar0125tipstar0125
提出日時 2023-12-07 14:19:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 274 ms / 2,000 ms
コード長 598 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 90,596 KB
最終ジャッジ日時 2023-12-07 14:19:25
合計ジャッジ時間 12,777 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,608 KB
testcase_01 AC 40 ms
55,608 KB
testcase_02 AC 41 ms
55,608 KB
testcase_03 AC 252 ms
90,596 KB
testcase_04 AC 174 ms
90,596 KB
testcase_05 AC 39 ms
55,608 KB
testcase_06 AC 181 ms
80,180 KB
testcase_07 AC 137 ms
77,852 KB
testcase_08 AC 145 ms
79,540 KB
testcase_09 AC 196 ms
79,260 KB
testcase_10 AC 165 ms
79,936 KB
testcase_11 AC 253 ms
84,940 KB
testcase_12 AC 125 ms
77,340 KB
testcase_13 AC 169 ms
78,876 KB
testcase_14 AC 189 ms
82,240 KB
testcase_15 AC 222 ms
81,536 KB
testcase_16 AC 171 ms
87,852 KB
testcase_17 AC 93 ms
76,956 KB
testcase_18 AC 106 ms
80,828 KB
testcase_19 AC 220 ms
84,212 KB
testcase_20 AC 208 ms
83,180 KB
testcase_21 AC 204 ms
83,556 KB
testcase_22 AC 157 ms
81,172 KB
testcase_23 AC 159 ms
80,776 KB
testcase_24 AC 102 ms
76,956 KB
testcase_25 AC 221 ms
83,568 KB
testcase_26 AC 162 ms
80,356 KB
testcase_27 AC 197 ms
84,248 KB
testcase_28 AC 270 ms
83,812 KB
testcase_29 AC 274 ms
85,124 KB
testcase_30 AC 216 ms
84,556 KB
testcase_31 AC 219 ms
83,184 KB
testcase_32 AC 167 ms
80,752 KB
testcase_33 AC 241 ms
84,276 KB
testcase_34 AC 229 ms
82,244 KB
testcase_35 AC 201 ms
85,932 KB
testcase_36 AC 235 ms
83,760 KB
testcase_37 AC 162 ms
80,636 KB
testcase_38 AC 209 ms
82,636 KB
testcase_39 AC 229 ms
81,552 KB
testcase_40 AC 226 ms
86,012 KB
testcase_41 AC 246 ms
85,944 KB
testcase_42 AC 158 ms
80,340 KB
testcase_43 AC 196 ms
82,356 KB
testcase_44 AC 153 ms
79,516 KB
testcase_45 AC 125 ms
77,980 KB
testcase_46 AC 221 ms
81,420 KB
testcase_47 AC 161 ms
78,748 KB
testcase_48 AC 155 ms
79,396 KB
testcase_49 AC 113 ms
77,344 KB
testcase_50 AC 170 ms
79,004 KB
testcase_51 AC 40 ms
55,608 KB
testcase_52 AC 132 ms
77,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N,M=list(map(int, input().split()))
G=[[] for _ in range(N)]
for _ in range(M):
    u,v=list(map(int, input().split()))
    G[u-1].append(v-1)

INF=1<<60

def bfs(s, g):
    d=[INF for _ in range(N)]
    Q=deque()
    d[s]=0
    Q.append(s)
    while Q:
        pos=Q.popleft()
        for nex in G[pos]:
            if d[pos]+1<d[nex]:
                d[nex]=d[pos]+1
                Q.append(nex)
    return d[g]


root1=bfs(0,N-2)+bfs(N-2,N-1)+bfs(N-1,0)
root2=bfs(0,N-1)+bfs(N-1,N-2)+bfs(N-2,0)
ans=min(root1,root2)
if ans>=INF:
    print(-1)
else:
    print(ans)
0