結果

問題 No.2565 はじめてのおつかい
ユーザー みあえみあえ
提出日時 2023-12-05 00:03:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 919 bytes
コンパイル時間 2,646 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 91,100 KB
最終ジャッジ日時 2023-12-05 00:03:21
合計ジャッジ時間 11,800 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,480 KB
testcase_01 AC 40 ms
55,480 KB
testcase_02 AC 39 ms
55,480 KB
testcase_03 AC 254 ms
91,084 KB
testcase_04 AC 159 ms
91,100 KB
testcase_05 AC 38 ms
55,608 KB
testcase_06 AC 200 ms
79,892 KB
testcase_07 AC 123 ms
77,704 KB
testcase_08 AC 128 ms
78,996 KB
testcase_09 AC 165 ms
78,716 KB
testcase_10 AC 149 ms
79,392 KB
testcase_11 AC 235 ms
84,716 KB
testcase_12 AC 105 ms
76,808 KB
testcase_13 AC 151 ms
78,204 KB
testcase_14 AC 185 ms
81,716 KB
testcase_15 AC 187 ms
81,268 KB
testcase_16 AC 147 ms
87,504 KB
testcase_17 AC 84 ms
76,924 KB
testcase_18 AC 89 ms
79,920 KB
testcase_19 AC 203 ms
83,208 KB
testcase_20 AC 184 ms
83,056 KB
testcase_21 AC 201 ms
83,192 KB
testcase_22 AC 133 ms
81,032 KB
testcase_23 AC 137 ms
80,764 KB
testcase_24 AC 86 ms
76,936 KB
testcase_25 AC 194 ms
83,460 KB
testcase_26 AC 144 ms
80,344 KB
testcase_27 AC 171 ms
84,012 KB
testcase_28 AC 240 ms
82,804 KB
testcase_29 AC 222 ms
85,504 KB
testcase_30 AC 187 ms
84,704 KB
testcase_31 AC 198 ms
83,460 KB
testcase_32 AC 147 ms
80,484 KB
testcase_33 AC 191 ms
84,296 KB
testcase_34 AC 231 ms
82,620 KB
testcase_35 AC 175 ms
86,084 KB
testcase_36 AC 208 ms
84,292 KB
testcase_37 AC 141 ms
80,624 KB
testcase_38 AC 194 ms
82,368 KB
testcase_39 AC 192 ms
81,152 KB
testcase_40 AC 209 ms
85,776 KB
testcase_41 AC 219 ms
86,164 KB
testcase_42 AC 146 ms
79,940 KB
testcase_43 AC 176 ms
81,832 KB
testcase_44 AC 141 ms
79,120 KB
testcase_45 AC 103 ms
77,308 KB
testcase_46 AC 214 ms
81,792 KB
testcase_47 AC 152 ms
78,336 KB
testcase_48 AC 148 ms
78,980 KB
testcase_49 AC 105 ms
76,808 KB
testcase_50 AC 166 ms
78,600 KB
testcase_51 AC 40 ms
55,608 KB
testcase_52 AC 130 ms
77,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def solve(N, M, G):
    def bfs(start):
        costs = [float("inf")] * N
        costs[start] = 0
        fifo = deque([start])
        while fifo:
            u = fifo.popleft()
            for v in G[u]:
                if costs[v] == float("inf"):
                    costs[v] = costs[u] + 1
                    fifo.append(v)

        return costs

    costs1 = bfs(0)
    costs2 = bfs(N - 2)
    costs3 = bfs(N - 1)

    ans = min(
        costs1[N - 2] + costs2[N - 1] + costs3[0],
        costs1[N - 1] + costs3[N - 2] + costs2[0],
    )
    if ans == float("inf"):
        ans = -1

    return ans


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


if __name__ == "__main__":
    main()
0