結果

問題 No.2565 はじめてのおつかい
ユーザー noriaokinoriaoki
提出日時 2023-12-02 16:34:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 257 ms / 2,000 ms
コード長 745 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 115,688 KB
最終ジャッジ日時 2023-12-02 16:34:49
合計ジャッジ時間 11,087 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,616 KB
testcase_01 AC 43 ms
55,616 KB
testcase_02 AC 34 ms
55,616 KB
testcase_03 AC 224 ms
115,688 KB
testcase_04 AC 181 ms
115,688 KB
testcase_05 AC 36 ms
55,616 KB
testcase_06 AC 193 ms
97,984 KB
testcase_07 AC 130 ms
87,336 KB
testcase_08 AC 157 ms
84,848 KB
testcase_09 AC 204 ms
96,668 KB
testcase_10 AC 167 ms
90,288 KB
testcase_11 AC 245 ms
104,384 KB
testcase_12 AC 117 ms
82,024 KB
testcase_13 AC 161 ms
89,404 KB
testcase_14 AC 206 ms
98,812 KB
testcase_15 AC 213 ms
99,220 KB
testcase_16 AC 158 ms
98,748 KB
testcase_17 AC 79 ms
79,080 KB
testcase_18 AC 79 ms
81,148 KB
testcase_19 AC 210 ms
103,840 KB
testcase_20 AC 191 ms
101,192 KB
testcase_21 AC 173 ms
100,804 KB
testcase_22 AC 121 ms
88,504 KB
testcase_23 AC 158 ms
91,628 KB
testcase_24 AC 101 ms
78,548 KB
testcase_25 AC 228 ms
102,032 KB
testcase_26 AC 157 ms
91,368 KB
testcase_27 AC 156 ms
96,968 KB
testcase_28 AC 222 ms
103,452 KB
testcase_29 AC 248 ms
109,152 KB
testcase_30 AC 213 ms
102,672 KB
testcase_31 AC 213 ms
101,728 KB
testcase_32 AC 145 ms
91,596 KB
testcase_33 AC 199 ms
102,328 KB
testcase_34 AC 224 ms
101,344 KB
testcase_35 AC 170 ms
104,180 KB
testcase_36 AC 257 ms
103,160 KB
testcase_37 AC 152 ms
91,608 KB
testcase_38 AC 201 ms
100,716 KB
testcase_39 AC 208 ms
95,724 KB
testcase_40 AC 190 ms
106,336 KB
testcase_41 AC 221 ms
110,168 KB
testcase_42 AC 174 ms
91,128 KB
testcase_43 AC 177 ms
99,884 KB
testcase_44 AC 151 ms
84,868 KB
testcase_45 AC 116 ms
81,916 KB
testcase_46 AC 210 ms
100,708 KB
testcase_47 AC 159 ms
91,124 KB
testcase_48 AC 154 ms
90,052 KB
testcase_49 AC 137 ms
82,144 KB
testcase_50 AC 173 ms
97,164 KB
testcase_51 AC 35 ms
55,616 KB
testcase_52 AC 148 ms
99,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
UV = [map(int, input().split()) for _ in range(M)]

graph = [[] for _ in range(N)]
for U, V in UV:
    U -= 1
    V -= 1
    graph[U].append(V)
import collections
def bfs(start, goal):
    q = collections.deque()
    q.append(start)
    reached = [0] * N
    while q:
        now = q.popleft()
        for g in graph[now]:
            if reached[g]:
                continue
            reached[g] = reached[now] + 1
            q.append(g)
    return reached[goal]

ans = 10**18
a = bfs(0, N-2)
b = bfs(N-2, N-1)
c = bfs(N-1, 0)
if a and b and c:
    ans = min(ans, a + b + c)
a = bfs(0, N-1)
b = bfs(N-1, N-2)
c = bfs(N-2, 0)
if a and b and c:
    ans = min(ans, a + b + c)

print(ans if ans != 10**18 else -1)
0