結果

問題 No.2565 はじめてのおつかい
ユーザー prd_xxxprd_xxx
提出日時 2023-12-02 15:28:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 700 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 93,472 KB
最終ジャッジ日時 2023-12-02 15:28:53
合計ジャッジ時間 9,419 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,612 KB
testcase_01 AC 41 ms
55,612 KB
testcase_02 AC 41 ms
55,612 KB
testcase_03 AC 170 ms
93,472 KB
testcase_04 AC 164 ms
93,472 KB
testcase_05 AC 41 ms
55,612 KB
testcase_06 AC 146 ms
85,624 KB
testcase_07 AC 106 ms
79,204 KB
testcase_08 AC 108 ms
80,300 KB
testcase_09 AC 139 ms
84,948 KB
testcase_10 AC 127 ms
83,052 KB
testcase_11 AC 191 ms
93,432 KB
testcase_12 AC 92 ms
79,268 KB
testcase_13 AC 130 ms
82,424 KB
testcase_14 AC 177 ms
88,244 KB
testcase_15 AC 158 ms
87,652 KB
testcase_16 AC 117 ms
90,356 KB
testcase_17 AC 69 ms
76,836 KB
testcase_18 AC 71 ms
79,032 KB
testcase_19 AC 182 ms
91,352 KB
testcase_20 AC 156 ms
87,040 KB
testcase_21 AC 155 ms
87,164 KB
testcase_22 AC 123 ms
84,852 KB
testcase_23 AC 125 ms
85,412 KB
testcase_24 AC 80 ms
77,864 KB
testcase_25 AC 173 ms
88,432 KB
testcase_26 AC 127 ms
85,024 KB
testcase_27 AC 140 ms
87,552 KB
testcase_28 AC 169 ms
91,476 KB
testcase_29 AC 182 ms
92,184 KB
testcase_30 AC 167 ms
91,820 KB
testcase_31 AC 173 ms
90,520 KB
testcase_32 AC 130 ms
84,232 KB
testcase_33 AC 173 ms
91,792 KB
testcase_34 AC 177 ms
89,752 KB
testcase_35 AC 135 ms
87,084 KB
testcase_36 AC 194 ms
90,888 KB
testcase_37 AC 130 ms
84,756 KB
testcase_38 AC 178 ms
89,636 KB
testcase_39 AC 156 ms
86,948 KB
testcase_40 AC 183 ms
92,184 KB
testcase_41 AC 182 ms
93,456 KB
testcase_42 AC 123 ms
84,656 KB
testcase_43 AC 160 ms
88,420 KB
testcase_44 AC 124 ms
80,320 KB
testcase_45 AC 92 ms
77,240 KB
testcase_46 AC 149 ms
84,892 KB
testcase_47 AC 125 ms
84,012 KB
testcase_48 AC 136 ms
82,688 KB
testcase_49 AC 97 ms
79,640 KB
testcase_50 AC 129 ms
83,140 KB
testcase_51 AC 41 ms
55,612 KB
testcase_52 AC 122 ms
85,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N,M = map(int,input().split())
UV = [tuple(map(int,input().split())) for _ in range(M)]

es = [[] for _ in range(N)]
for u,v in UV:
    u,v = u-1,v-1
    es[u].append(v)

from collections import deque
INF = 10**18
def bfs(s,g):
    dist = [INF] * N
    dist[s] = 0
    q = deque([s])
    while q:
        v = q.popleft()
        if v==g:
            return dist[v]
        for to in es[v]:
            if dist[to] <= dist[v] + 1: continue
            dist[to] = dist[v] + 1
            q.append(to)
    return INF

ans1 = bfs(0,N-2) + bfs(N-2,N-1) + bfs(N-1,0)
ans2 = bfs(0,N-1) + bfs(N-1,N-2) + bfs(N-2,0)

ans = min(ans1, ans2)
print(-1 if ans >= INF else ans)
0