結果

問題 No.2565 はじめてのおつかい
ユーザー AngrySadEightAngrySadEight
提出日時 2023-11-24 01:15:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 804 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 92,292 KB
最終ジャッジ日時 2023-11-24 01:15:38
合計ジャッジ時間 13,726 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,608 KB
testcase_01 AC 43 ms
55,608 KB
testcase_02 AC 43 ms
55,608 KB
testcase_03 AC 272 ms
92,292 KB
testcase_04 AC 170 ms
92,292 KB
testcase_05 AC 42 ms
55,608 KB
testcase_06 AC 205 ms
80,668 KB
testcase_07 AC 141 ms
77,964 KB
testcase_08 AC 161 ms
79,516 KB
testcase_09 AC 182 ms
79,236 KB
testcase_10 AC 166 ms
80,296 KB
testcase_11 AC 290 ms
85,140 KB
testcase_12 AC 113 ms
77,196 KB
testcase_13 AC 159 ms
78,980 KB
testcase_14 AC 211 ms
82,492 KB
testcase_15 AC 214 ms
82,044 KB
testcase_16 AC 160 ms
87,748 KB
testcase_17 AC 122 ms
77,196 KB
testcase_18 AC 99 ms
80,056 KB
testcase_19 AC 225 ms
83,856 KB
testcase_20 AC 203 ms
83,320 KB
testcase_21 AC 182 ms
83,456 KB
testcase_22 AC 138 ms
81,168 KB
testcase_23 AC 158 ms
81,028 KB
testcase_24 AC 97 ms
77,068 KB
testcase_25 AC 219 ms
84,364 KB
testcase_26 AC 157 ms
80,480 KB
testcase_27 AC 180 ms
84,148 KB
testcase_28 AC 233 ms
83,580 KB
testcase_29 AC 236 ms
86,432 KB
testcase_30 AC 195 ms
84,840 KB
testcase_31 AC 214 ms
84,236 KB
testcase_32 AC 154 ms
80,748 KB
testcase_33 AC 205 ms
84,560 KB
testcase_34 AC 235 ms
83,396 KB
testcase_35 AC 182 ms
86,220 KB
testcase_36 AC 239 ms
85,324 KB
testcase_37 AC 152 ms
80,888 KB
testcase_38 AC 210 ms
82,632 KB
testcase_39 AC 205 ms
81,800 KB
testcase_40 AC 200 ms
86,040 KB
testcase_41 AC 239 ms
87,256 KB
testcase_42 AC 153 ms
80,204 KB
testcase_43 AC 190 ms
82,352 KB
testcase_44 AC 150 ms
79,512 KB
testcase_45 AC 111 ms
77,444 KB
testcase_46 AC 224 ms
82,568 KB
testcase_47 AC 162 ms
78,860 KB
testcase_48 AC 174 ms
79,884 KB
testcase_49 AC 113 ms
77,196 KB
testcase_50 AC 169 ms
78,476 KB
testcase_51 AC 43 ms
55,608 KB
testcase_52 AC 121 ms
76,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

INF = 10 ** 17

N, M = map(int, input().split())
graph = [[] for _ in range(N)]
for i in range(M):
    u, v = map(int, input().split())
    graph[u - 1].append(v - 1)


def bfs(v):
    dq = deque()
    isvisited = [False for _ in range(N)]
    dist = [INF for _ in range(N)]
    isvisited[v] = True
    dist[v] = 0
    dq.append(v)
    while len(dq):
        p = dq.popleft()
        for x in graph[p]:
            if isvisited[x]:
                continue
            isvisited[x] = True
            dq.append(x)
            dist[x] = dist[p] + 1
    return dist


dist1 = bfs(0)
distn1 = bfs(N - 2)
distn = bfs(N - 1)

ans = min(dist1[N - 2] + distn1[N - 1] + distn[0],
          dist1[N - 1] + distn[N - 2] + distn1[0])
if ans >= INF:
    print(-1)
else:
    print(ans)
0