結果

問題 No.2565 はじめてのおつかい
ユーザー かもめのうでかもめのうで
提出日時 2023-12-02 20:02:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 680 bytes
コンパイル時間 378 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,600 KB
最終ジャッジ日時 2023-12-02 20:03:03
合計ジャッジ時間 9,871 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,480 KB
testcase_01 AC 44 ms
55,480 KB
testcase_02 AC 43 ms
55,480 KB
testcase_03 AC 223 ms
89,600 KB
testcase_04 AC 154 ms
89,600 KB
testcase_05 AC 42 ms
55,480 KB
testcase_06 AC 183 ms
79,384 KB
testcase_07 AC 119 ms
77,700 KB
testcase_08 AC 122 ms
78,616 KB
testcase_09 AC 147 ms
78,720 KB
testcase_10 AC 134 ms
78,884 KB
testcase_11 AC 203 ms
83,816 KB
testcase_12 AC 102 ms
76,420 KB
testcase_13 AC 126 ms
78,208 KB
testcase_14 AC 164 ms
81,208 KB
testcase_15 AC 180 ms
80,504 KB
testcase_16 AC 159 ms
85,440 KB
testcase_17 AC 88 ms
76,932 KB
testcase_18 AC 93 ms
79,028 KB
testcase_19 AC 184 ms
82,316 KB
testcase_20 AC 174 ms
82,036 KB
testcase_21 AC 172 ms
82,044 KB
testcase_22 AC 130 ms
80,140 KB
testcase_23 AC 134 ms
79,872 KB
testcase_24 AC 99 ms
76,932 KB
testcase_25 AC 177 ms
82,184 KB
testcase_26 AC 137 ms
79,580 KB
testcase_27 AC 165 ms
82,608 KB
testcase_28 AC 194 ms
82,040 KB
testcase_29 AC 212 ms
84,508 KB
testcase_30 AC 176 ms
83,172 KB
testcase_31 AC 203 ms
82,312 KB
testcase_32 AC 138 ms
79,720 KB
testcase_33 AC 183 ms
82,892 KB
testcase_34 AC 193 ms
81,472 KB
testcase_35 AC 184 ms
84,296 KB
testcase_36 AC 197 ms
82,888 KB
testcase_37 AC 136 ms
79,860 KB
testcase_38 AC 179 ms
81,476 KB
testcase_39 AC 198 ms
80,516 KB
testcase_40 AC 189 ms
84,372 KB
testcase_41 AC 205 ms
85,076 KB
testcase_42 AC 137 ms
79,304 KB
testcase_43 AC 163 ms
81,068 KB
testcase_44 AC 128 ms
78,612 KB
testcase_45 AC 99 ms
77,312 KB
testcase_46 AC 178 ms
80,644 KB
testcase_47 AC 147 ms
78,340 KB
testcase_48 AC 134 ms
78,600 KB
testcase_49 AC 101 ms
76,420 KB
testcase_50 AC 150 ms
78,084 KB
testcase_51 AC 42 ms
55,480 KB
testcase_52 AC 120 ms
76,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n, m = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(m):
    v, u = map(int, input().split())
    v -= 1
    u -= 1
    g[v].append(u)

def culc_dist(st: int, g: list):
    dist = [1<<60]*n
    q = deque()
    q.append(st)
    dist[st] = 0
    while q:
        v = q.popleft()
        for nv in g[v]:
            if dist[nv] == 1<<60:
                dist[nv] = dist[v] + 1
                q.append(nv)
    return dist

dist1 = culc_dist(0, g)
dist2 = culc_dist(n-2, g)
dist3 = culc_dist(n-1, g)
ans1 = dist1[n-2]+dist2[n-1]+dist3[0]
ans2 = dist1[n-1]+dist3[n-2]+dist2[0]
ans = min(ans1, ans2)
print(ans if ans < 1<<60 else -1)
0