結果

問題 No.2565 はじめてのおつかい
ユーザー poeMoonpoeMoon
提出日時 2023-12-06 15:20:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 322 ms / 2,000 ms
コード長 1,313 bytes
コンパイル時間 1,230 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 96,460 KB
最終ジャッジ日時 2023-12-06 15:20:34
合計ジャッジ時間 12,797 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,604 KB
testcase_01 AC 41 ms
55,608 KB
testcase_02 AC 41 ms
55,608 KB
testcase_03 AC 230 ms
96,460 KB
testcase_04 AC 186 ms
95,696 KB
testcase_05 AC 41 ms
55,608 KB
testcase_06 AC 184 ms
80,764 KB
testcase_07 AC 132 ms
78,084 KB
testcase_08 AC 158 ms
80,764 KB
testcase_09 AC 178 ms
80,124 KB
testcase_10 AC 156 ms
80,508 KB
testcase_11 AC 310 ms
88,712 KB
testcase_12 AC 100 ms
76,804 KB
testcase_13 AC 185 ms
79,740 KB
testcase_14 AC 270 ms
84,348 KB
testcase_15 AC 228 ms
82,684 KB
testcase_16 AC 147 ms
89,232 KB
testcase_17 AC 87 ms
77,308 KB
testcase_18 AC 91 ms
80,380 KB
testcase_19 AC 296 ms
87,420 KB
testcase_20 AC 257 ms
85,756 KB
testcase_21 AC 212 ms
85,116 KB
testcase_22 AC 147 ms
82,172 KB
testcase_23 AC 189 ms
82,044 KB
testcase_24 AC 98 ms
77,060 KB
testcase_25 AC 313 ms
86,972 KB
testcase_26 AC 174 ms
82,044 KB
testcase_27 AC 181 ms
87,292 KB
testcase_28 AC 241 ms
87,676 KB
testcase_29 AC 258 ms
90,564 KB
testcase_30 AC 212 ms
87,208 KB
testcase_31 AC 243 ms
86,908 KB
testcase_32 AC 165 ms
82,300 KB
testcase_33 AC 262 ms
87,368 KB
testcase_34 AC 266 ms
83,964 KB
testcase_35 AC 192 ms
87,968 KB
testcase_36 AC 322 ms
87,164 KB
testcase_37 AC 177 ms
82,300 KB
testcase_38 AC 277 ms
85,244 KB
testcase_39 AC 251 ms
83,964 KB
testcase_40 AC 230 ms
88,424 KB
testcase_41 AC 242 ms
89,324 KB
testcase_42 AC 153 ms
80,892 KB
testcase_43 AC 224 ms
84,604 KB
testcase_44 AC 180 ms
80,124 KB
testcase_45 AC 114 ms
77,820 KB
testcase_46 AC 203 ms
83,836 KB
testcase_47 AC 142 ms
78,976 KB
testcase_48 AC 164 ms
80,380 KB
testcase_49 AC 110 ms
77,444 KB
testcase_50 AC 147 ms
78,596 KB
testcase_51 AC 41 ms
55,604 KB
testcase_52 AC 124 ms
77,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from collections import deque
#import itertools

def bfs(start, end):
    cnt = 0
    nows = deque([start])
    visited = [False for i in range(N)]
    while True:
        if len(nows) == 0:
            return -1
        cnt += 1
        for _ in range(len(nows)):
            now = nows.popleft()
            visited[now] = True
            for after in graph[now]:
                if after == end:
                    return cnt
                if not visited[after]:
                    nows.append(after)

N, M = map(int, input().split())
graph = defaultdict(list)
for _ in range(M):
    u, v = map(int, input().split())
    graph[u - 1].append(v - 1)
#for i in range(N):
#    print(str(i) + ": ", end = "")
#    print(*graph[i])

# route1: 0 -> N - 2 -> N - 1 -> 0
sec1 = bfs(0, N - 2)
sec2 = bfs(N - 2, N - 1)
sec3 = bfs(N - 1, 0)
route1 = sec1 + sec2 + sec3 if sec1 >= 0 and sec2 >= 0 and sec3 >= 0 else -1
# route2: 0 -> N - 1 -> N - 2 -> 0
sec1 = bfs(0, N - 1)
sec2 = bfs(N - 1, N - 2)
sec3 = bfs(N - 2, 0)
route2 = sec1 + sec2 + sec3 if sec1 >= 0 and sec2 >= 0 and sec3 >= 0 else -1
if route1 >= 0 and route2 >= 0:
    print(min(route1, route2))
elif route1 >= 0 and route2 < 0:
    print(route1)
elif route1 < 0 and route2 >= 0:
    print(route2)
else:
    print(-1)
0