結果

問題 No.2565 はじめてのおつかい
ユーザー n_nan_na
提出日時 2024-01-05 00:33:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 802 bytes
コンパイル時間 2,324 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 91,392 KB
最終ジャッジ日時 2024-01-05 00:33:35
合計ジャッジ時間 10,819 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,608 KB
testcase_01 AC 38 ms
55,608 KB
testcase_02 AC 38 ms
55,608 KB
testcase_03 AC 217 ms
91,388 KB
testcase_04 AC 159 ms
91,392 KB
testcase_05 AC 40 ms
55,608 KB
testcase_06 AC 176 ms
80,024 KB
testcase_07 AC 124 ms
77,704 KB
testcase_08 AC 141 ms
79,256 KB
testcase_09 AC 166 ms
78,976 KB
testcase_10 AC 148 ms
80,036 KB
testcase_11 AC 227 ms
84,408 KB
testcase_12 AC 108 ms
77,064 KB
testcase_13 AC 144 ms
78,336 KB
testcase_14 AC 188 ms
82,104 KB
testcase_15 AC 194 ms
82,040 KB
testcase_16 AC 164 ms
87,488 KB
testcase_17 AC 85 ms
76,936 KB
testcase_18 AC 91 ms
79,924 KB
testcase_19 AC 199 ms
83,596 KB
testcase_20 AC 180 ms
83,188 KB
testcase_21 AC 164 ms
83,196 KB
testcase_22 AC 124 ms
81,036 KB
testcase_23 AC 144 ms
80,768 KB
testcase_24 AC 86 ms
76,936 KB
testcase_25 AC 198 ms
84,104 KB
testcase_26 AC 151 ms
80,348 KB
testcase_27 AC 162 ms
83,888 KB
testcase_28 AC 200 ms
83,320 KB
testcase_29 AC 212 ms
85,680 KB
testcase_30 AC 175 ms
84,708 KB
testcase_31 AC 192 ms
83,464 KB
testcase_32 AC 145 ms
80,488 KB
testcase_33 AC 190 ms
84,428 KB
testcase_34 AC 213 ms
83,264 KB
testcase_35 AC 170 ms
86,088 KB
testcase_36 AC 216 ms
85,064 KB
testcase_37 AC 142 ms
80,628 KB
testcase_38 AC 185 ms
82,500 KB
testcase_39 AC 217 ms
81,412 KB
testcase_40 AC 188 ms
85,908 KB
testcase_41 AC 214 ms
86,476 KB
testcase_42 AC 143 ms
80,072 KB
testcase_43 AC 172 ms
82,092 KB
testcase_44 AC 143 ms
79,124 KB
testcase_45 AC 104 ms
77,312 KB
testcase_46 AC 211 ms
82,308 KB
testcase_47 AC 145 ms
78,600 KB
testcase_48 AC 150 ms
79,624 KB
testcase_49 AC 109 ms
77,064 KB
testcase_50 AC 155 ms
78,344 KB
testcase_51 AC 40 ms
55,608 KB
testcase_52 AC 119 ms
76,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

INF = float("inf")
def bfs(start, edge): #始点,edge
    _N = len(edge)
    dist = [INF]*_N
    dist[start] = 0
    deq = deque()
    deq.append(start)
    while len(deq) > 0:
        crr = deq.popleft()
        for nxt in edge[crr]:
            if dist[nxt] == INF:
                dist[nxt] = dist[crr] + 1
                deq.append(nxt)
    return dist

#--------------------------------------------------
N,M = map(int,input().split())

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

b1 = bfs(0,E)
b2 = bfs(N-2,E)
b3 = bfs(N-1,E)

r1 = b1[N-2] + b2[N-1] + b3[0]
r2 = b1[N-1] + b3[N-2] + b2[0]
r3 = b1[N-2] + b2[0] + b1[N-1] + b3[0]

ans = min(r1,r2,r3)
if ans == INF:
    print(-1)
else:
    print(ans)
    
0