結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
55,488 KB
testcase_01 AC 36 ms
55,488 KB
testcase_02 AC 42 ms
55,488 KB
testcase_03 AC 165 ms
89,468 KB
testcase_04 AC 126 ms
89,596 KB
testcase_05 AC 37 ms
55,488 KB
testcase_06 AC 133 ms
79,112 KB
testcase_07 AC 99 ms
77,552 KB
testcase_08 AC 99 ms
78,596 KB
testcase_09 AC 118 ms
78,704 KB
testcase_10 AC 108 ms
78,608 KB
testcase_11 AC 149 ms
83,800 KB
testcase_12 AC 102 ms
76,016 KB
testcase_13 AC 109 ms
77,808 KB
testcase_14 AC 133 ms
81,188 KB
testcase_15 AC 129 ms
80,488 KB
testcase_16 AC 127 ms
85,180 KB
testcase_17 AC 76 ms
76,784 KB
testcase_18 AC 81 ms
78,752 KB
testcase_19 AC 146 ms
82,040 KB
testcase_20 AC 139 ms
81,760 KB
testcase_21 AC 156 ms
81,768 KB
testcase_22 AC 102 ms
79,868 KB
testcase_23 AC 101 ms
79,600 KB
testcase_24 AC 77 ms
76,912 KB
testcase_25 AC 135 ms
81,908 KB
testcase_26 AC 107 ms
79,308 KB
testcase_27 AC 128 ms
82,328 KB
testcase_28 AC 149 ms
81,764 KB
testcase_29 AC 164 ms
84,484 KB
testcase_30 AC 132 ms
82,892 KB
testcase_31 AC 165 ms
81,908 KB
testcase_32 AC 108 ms
79,448 KB
testcase_33 AC 144 ms
82,740 KB
testcase_34 WA -
testcase_35 AC 145 ms
84,016 KB
testcase_36 AC 145 ms
82,608 KB
testcase_37 AC 110 ms
79,588 KB
testcase_38 AC 142 ms
81,200 KB
testcase_39 AC 153 ms
80,500 KB
testcase_40 AC 155 ms
84,348 KB
testcase_41 AC 161 ms
85,052 KB
testcase_42 AC 113 ms
79,032 KB
testcase_43 AC 123 ms
80,920 KB
testcase_44 AC 103 ms
78,596 KB
testcase_45 AC 81 ms
77,040 KB
testcase_46 AC 138 ms
80,368 KB
testcase_47 AC 113 ms
78,320 KB
testcase_48 AC 126 ms
78,328 KB
testcase_49 AC 88 ms
76,144 KB
testcase_50 AC 123 ms
77,808 KB
testcase_51 AC 36 ms
55,488 KB
testcase_52 AC 103 ms
76,384 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-3] == -1 or dist1[n-1] == -1 or dist2[0] == -1):
    print(-1)
else:
    print(min(dist1[n-2]+dist2[n-1]+dist3[0], dist1[n-1]+dist3[n-2]+dist2[0]))
0