結果

問題 No.2565 はじめてのおつかい
ユーザー n_nan_na
提出日時 2024-01-05 00:29:51
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 802 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 91,388 KB
最終ジャッジ日時 2024-01-05 00:30:06
合計ジャッジ時間 12,464 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,608 KB
testcase_01 AC 41 ms
55,608 KB
testcase_02 AC 40 ms
55,608 KB
testcase_03 AC 203 ms
91,388 KB
testcase_04 AC 159 ms
91,380 KB
testcase_05 AC 41 ms
55,608 KB
testcase_06 WA -
testcase_07 AC 125 ms
77,704 KB
testcase_08 WA -
testcase_09 AC 162 ms
78,976 KB
testcase_10 WA -
testcase_11 AC 215 ms
84,404 KB
testcase_12 AC 110 ms
77,064 KB
testcase_13 AC 139 ms
78,336 KB
testcase_14 AC 184 ms
82,104 KB
testcase_15 AC 194 ms
82,040 KB
testcase_16 AC 157 ms
87,488 KB
testcase_17 AC 86 ms
76,932 KB
testcase_18 AC 90 ms
79,924 KB
testcase_19 AC 197 ms
83,212 KB
testcase_20 AC 187 ms
83,188 KB
testcase_21 AC 182 ms
83,196 KB
testcase_22 AC 128 ms
81,036 KB
testcase_23 AC 141 ms
80,768 KB
testcase_24 AC 87 ms
76,936 KB
testcase_25 AC 182 ms
84,104 KB
testcase_26 AC 141 ms
80,348 KB
testcase_27 AC 150 ms
83,888 KB
testcase_28 AC 187 ms
83,320 KB
testcase_29 WA -
testcase_30 AC 166 ms
84,708 KB
testcase_31 AC 204 ms
83,464 KB
testcase_32 AC 138 ms
80,488 KB
testcase_33 AC 183 ms
84,428 KB
testcase_34 AC 204 ms
83,264 KB
testcase_35 AC 170 ms
86,088 KB
testcase_36 AC 205 ms
85,064 KB
testcase_37 AC 134 ms
80,628 KB
testcase_38 AC 197 ms
82,500 KB
testcase_39 AC 182 ms
81,412 KB
testcase_40 AC 182 ms
85,908 KB
testcase_41 AC 206 ms
86,492 KB
testcase_42 AC 138 ms
80,072 KB
testcase_43 AC 162 ms
82,092 KB
testcase_44 AC 150 ms
79,124 KB
testcase_45 AC 109 ms
77,312 KB
testcase_46 AC 211 ms
82,308 KB
testcase_47 AC 153 ms
78,600 KB
testcase_48 AC 156 ms
79,624 KB
testcase_49 AC 114 ms
77,064 KB
testcase_50 AC 162 ms
78,344 KB
testcase_51 AC 42 ms
55,608 KB
testcase_52 AC 126 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] + b1[0]

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