結果

問題 No.2565 はじめてのおつかい
ユーザー ntudantuda
提出日時 2023-12-04 20:22:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 679 bytes
コンパイル時間 560 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 100,368 KB
最終ジャッジ日時 2023-12-04 20:22:50
合計ジャッジ時間 10,818 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,460 KB
testcase_01 AC 34 ms
53,460 KB
testcase_02 AC 33 ms
53,460 KB
testcase_03 AC 228 ms
100,368 KB
testcase_04 AC 192 ms
100,368 KB
testcase_05 AC 34 ms
53,460 KB
testcase_06 AC 147 ms
86,120 KB
testcase_07 AC 110 ms
80,596 KB
testcase_08 AC 110 ms
82,460 KB
testcase_09 AC 139 ms
85,828 KB
testcase_10 AC 119 ms
81,756 KB
testcase_11 AC 183 ms
94,568 KB
testcase_12 AC 97 ms
78,740 KB
testcase_13 AC 118 ms
80,616 KB
testcase_14 AC 176 ms
88,740 KB
testcase_15 AC 157 ms
88,660 KB
testcase_16 AC 141 ms
91,876 KB
testcase_17 AC 79 ms
76,692 KB
testcase_18 AC 84 ms
78,888 KB
testcase_19 AC 176 ms
92,360 KB
testcase_20 AC 169 ms
90,736 KB
testcase_21 AC 149 ms
88,300 KB
testcase_22 AC 139 ms
82,532 KB
testcase_23 AC 133 ms
85,524 KB
testcase_24 AC 79 ms
76,800 KB
testcase_25 AC 166 ms
91,232 KB
testcase_26 AC 118 ms
82,192 KB
testcase_27 AC 177 ms
87,792 KB
testcase_28 AC 193 ms
92,484 KB
testcase_29 AC 166 ms
90,120 KB
testcase_30 AC 152 ms
87,836 KB
testcase_31 AC 193 ms
91,400 KB
testcase_32 AC 121 ms
82,296 KB
testcase_33 AC 168 ms
92,416 KB
testcase_34 AC 159 ms
90,376 KB
testcase_35 AC 154 ms
88,476 KB
testcase_36 AC 169 ms
91,384 KB
testcase_37 AC 120 ms
82,308 KB
testcase_38 AC 166 ms
90,644 KB
testcase_39 AC 169 ms
87,700 KB
testcase_40 AC 161 ms
89,992 KB
testcase_41 AC 163 ms
90,496 KB
testcase_42 AC 126 ms
85,024 KB
testcase_43 AC 163 ms
89,300 KB
testcase_44 AC 122 ms
82,736 KB
testcase_45 AC 94 ms
79,656 KB
testcase_46 AC 152 ms
86,284 KB
testcase_47 AC 151 ms
82,588 KB
testcase_48 AC 116 ms
81,520 KB
testcase_49 AC 98 ms
78,728 KB
testcase_50 AC 144 ms
84,532 KB
testcase_51 AC 35 ms
53,460 KB
testcase_52 AC 143 ms
86,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

INF = 10 ** 6
def bfs(a, b):
    nv = [True] * (N + 1)
    q = [a]
    cnt = 0
    while q:
        q2 = []
        while q:
            x = q.pop(-1)
            if x == b:
                return cnt
            for y in E[x]:
                if nv[y]:
                    nv[y] = False
                    q2.append(y)
        cnt += 1
        q, q2 = q2, q
    return INF

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