結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,616 KB
testcase_01 AC 39 ms
55,616 KB
testcase_02 AC 39 ms
55,616 KB
testcase_03 AC 209 ms
89,724 KB
testcase_04 AC 145 ms
89,724 KB
testcase_05 AC 39 ms
55,616 KB
testcase_06 AC 144 ms
79,240 KB
testcase_07 AC 111 ms
77,680 KB
testcase_08 AC 112 ms
78,724 KB
testcase_09 AC 136 ms
78,832 KB
testcase_10 AC 122 ms
78,736 KB
testcase_11 AC 173 ms
83,928 KB
testcase_12 AC 109 ms
76,144 KB
testcase_13 AC 115 ms
77,936 KB
testcase_14 AC 146 ms
81,316 KB
testcase_15 AC 149 ms
80,616 KB
testcase_16 AC 135 ms
85,308 KB
testcase_17 AC 82 ms
76,912 KB
testcase_18 AC 87 ms
78,880 KB
testcase_19 AC 160 ms
82,168 KB
testcase_20 AC 153 ms
81,888 KB
testcase_21 AC 174 ms
81,896 KB
testcase_22 AC 116 ms
79,996 KB
testcase_23 AC 119 ms
79,728 KB
testcase_24 AC 84 ms
77,040 KB
testcase_25 AC 152 ms
82,036 KB
testcase_26 WA -
testcase_27 AC 145 ms
82,456 KB
testcase_28 AC 165 ms
81,892 KB
testcase_29 WA -
testcase_30 AC 154 ms
83,020 KB
testcase_31 AC 161 ms
82,036 KB
testcase_32 AC 126 ms
79,576 KB
testcase_33 AC 159 ms
82,868 KB
testcase_34 AC 161 ms
81,324 KB
testcase_35 AC 162 ms
84,144 KB
testcase_36 AC 190 ms
82,736 KB
testcase_37 AC 122 ms
79,716 KB
testcase_38 AC 154 ms
81,328 KB
testcase_39 WA -
testcase_40 AC 166 ms
84,476 KB
testcase_41 AC 176 ms
85,180 KB
testcase_42 AC 123 ms
79,160 KB
testcase_43 AC 144 ms
81,048 KB
testcase_44 AC 143 ms
78,724 KB
testcase_45 AC 95 ms
77,168 KB
testcase_46 AC 165 ms
80,496 KB
testcase_47 AC 128 ms
78,448 KB
testcase_48 AC 122 ms
78,456 KB
testcase_49 AC 96 ms
76,144 KB
testcase_50 AC 137 ms
77,936 KB
testcase_51 AC 39 ms
55,616 KB
testcase_52 AC 116 ms
76,512 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:
    print(-1)
else:
    print(min(dist1[n-2]+dist2[n-1]+dist3[0], dist1[n-1]+dist3[n-2]+dist2[0]))
0