結果

問題 No.2565 はじめてのおつかい
ユーザー かもめのうでかもめのうで
提出日時 2023-12-02 16:37:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,525 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,724 KB
最終ジャッジ日時 2023-12-02 16:37:19
合計ジャッジ時間 7,865 ms
ジャッジサーバーID
(参考情報)
judge15 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,612 KB
testcase_01 AC 38 ms
55,616 KB
testcase_02 AC 36 ms
55,612 KB
testcase_03 AC 155 ms
89,724 KB
testcase_04 AC 126 ms
89,724 KB
testcase_05 AC 34 ms
55,616 KB
testcase_06 AC 148 ms
79,240 KB
testcase_07 AC 100 ms
77,680 KB
testcase_08 AC 100 ms
78,724 KB
testcase_09 AC 118 ms
78,832 KB
testcase_10 AC 105 ms
78,736 KB
testcase_11 AC 151 ms
83,928 KB
testcase_12 AC 84 ms
76,144 KB
testcase_13 AC 100 ms
77,936 KB
testcase_14 AC 126 ms
81,188 KB
testcase_15 AC 152 ms
80,612 KB
testcase_16 AC 117 ms
85,308 KB
testcase_17 AC 96 ms
76,784 KB
testcase_18 AC 78 ms
78,880 KB
testcase_19 AC 136 ms
82,168 KB
testcase_20 AC 129 ms
81,888 KB
testcase_21 AC 131 ms
81,896 KB
testcase_22 AC 103 ms
79,996 KB
testcase_23 AC 105 ms
79,728 KB
testcase_24 AC 78 ms
77,040 KB
testcase_25 AC 161 ms
82,036 KB
testcase_26 AC 107 ms
79,436 KB
testcase_27 AC 133 ms
82,456 KB
testcase_28 AC 143 ms
81,892 KB
testcase_29 AC 157 ms
84,612 KB
testcase_30 AC 135 ms
83,020 KB
testcase_31 AC 131 ms
82,036 KB
testcase_32 AC 108 ms
79,576 KB
testcase_33 AC 134 ms
82,740 KB
testcase_34 AC 141 ms
81,324 KB
testcase_35 AC 137 ms
84,144 KB
testcase_36 AC 142 ms
82,736 KB
testcase_37 AC 117 ms
79,716 KB
testcase_38 AC 136 ms
81,328 KB
testcase_39 AC 137 ms
80,628 KB
testcase_40 AC 147 ms
84,476 KB
testcase_41 AC 160 ms
85,180 KB
testcase_42 AC 111 ms
79,160 KB
testcase_43 AC 128 ms
81,048 KB
testcase_44 AC 119 ms
78,724 KB
testcase_45 AC 86 ms
77,168 KB
testcase_46 AC 130 ms
80,496 KB
testcase_47 AC 111 ms
78,448 KB
testcase_48 AC 104 ms
78,456 KB
testcase_49 AC 85 ms
76,272 KB
testcase_50 AC 118 ms
77,936 KB
testcase_51 AC 36 ms
55,612 KB
testcase_52 AC 103 ms
76,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# n, m = map(int, input().split())
# c = list(map(int, input().split()))

# cnt = [set() for _ in range(n)]
# for i in range(n):
#     cnt[c[i]-1].add(i)

# s = set()
# edge = [0]*n
# for _ in range(m):
#     v, u = map(int, input().split())
#     v -= 1
#     u -= 1
#     if c[v] == c[u]:
#         if (v, u) not in s and (u, v) not in s:
#             edge[c[v]-1] += 1
#             s.add((v, u))
#             s.add((u, v))

# ans = 0
# for e, t in zip(edge, cnt):
#     if len(t) == 0:
#         continue
#     ans += max(len(t)-1-e, 0)
# print(ans)

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)
from collections import deque

def culc_dist(st: int, g: list):
    dist = [-1]*n
    q = deque()
    q.append(st)
    dist[st] = 0
    while q:
        v = q.popleft()
        for nv in g[v]:
            if dist[nv] == -1:
                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)
if (dist1[n-2] == -1 or dist2[n-1] == -1 or dist3[0] == -1) and (dist3[n-2] == -1 or dist1[n-1] == -1 or dist2[0] == -1):
    print(-1)
elif dist1[n-2] == -1 or dist2[n-1] == -1 or dist3[0] == -1:
    print(dist1[n-1]+dist3[n-2]+dist2[0])
elif dist3[n-2] == -1 or dist1[n-1] == -1 or dist2[0] == -1:
    print(dist1[n-2]+dist2[n-1]+dist3[0])
else:
    print(min(dist1[n-2]+dist2[n-1]+dist3[0], dist1[n-1]+dist3[n-2]+dist2[0]))
0