結果
問題 | No.2565 はじめてのおつかい |
ユーザー | poeMoon |
提出日時 | 2023-12-06 15:20:20 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 299 ms / 2,000 ms |
コード長 | 1,313 bytes |
コンパイル時間 | 380 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 97,116 KB |
最終ジャッジ日時 | 2024-09-27 01:23:53 |
合計ジャッジ時間 | 12,109 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,016 KB |
testcase_01 | AC | 43 ms
53,632 KB |
testcase_02 | AC | 43 ms
54,016 KB |
testcase_03 | AC | 215 ms
97,116 KB |
testcase_04 | AC | 187 ms
95,964 KB |
testcase_05 | AC | 45 ms
53,504 KB |
testcase_06 | AC | 173 ms
81,024 KB |
testcase_07 | AC | 128 ms
78,592 KB |
testcase_08 | AC | 158 ms
81,280 KB |
testcase_09 | AC | 177 ms
80,384 KB |
testcase_10 | AC | 154 ms
80,896 KB |
testcase_11 | AC | 299 ms
88,960 KB |
testcase_12 | AC | 110 ms
76,928 KB |
testcase_13 | AC | 172 ms
79,872 KB |
testcase_14 | AC | 241 ms
84,844 KB |
testcase_15 | AC | 212 ms
83,200 KB |
testcase_16 | AC | 149 ms
89,504 KB |
testcase_17 | AC | 96 ms
77,440 KB |
testcase_18 | AC | 102 ms
80,768 KB |
testcase_19 | AC | 246 ms
87,936 KB |
testcase_20 | AC | 235 ms
86,272 KB |
testcase_21 | AC | 198 ms
85,516 KB |
testcase_22 | AC | 152 ms
82,560 KB |
testcase_23 | AC | 187 ms
82,560 KB |
testcase_24 | AC | 106 ms
77,312 KB |
testcase_25 | AC | 264 ms
87,416 KB |
testcase_26 | AC | 169 ms
82,304 KB |
testcase_27 | AC | 187 ms
87,680 KB |
testcase_28 | AC | 220 ms
87,936 KB |
testcase_29 | AC | 237 ms
91,232 KB |
testcase_30 | AC | 202 ms
87,612 KB |
testcase_31 | AC | 217 ms
87,168 KB |
testcase_32 | AC | 163 ms
82,688 KB |
testcase_33 | AC | 232 ms
87,776 KB |
testcase_34 | AC | 235 ms
84,352 KB |
testcase_35 | AC | 180 ms
88,628 KB |
testcase_36 | AC | 293 ms
87,680 KB |
testcase_37 | AC | 171 ms
82,560 KB |
testcase_38 | AC | 250 ms
86,016 KB |
testcase_39 | AC | 209 ms
84,224 KB |
testcase_40 | AC | 214 ms
88,824 KB |
testcase_41 | AC | 222 ms
89,720 KB |
testcase_42 | AC | 150 ms
81,280 KB |
testcase_43 | AC | 209 ms
84,864 KB |
testcase_44 | AC | 164 ms
80,640 KB |
testcase_45 | AC | 120 ms
78,336 KB |
testcase_46 | AC | 192 ms
84,224 KB |
testcase_47 | AC | 145 ms
79,232 KB |
testcase_48 | AC | 162 ms
80,896 KB |
testcase_49 | AC | 131 ms
77,696 KB |
testcase_50 | AC | 172 ms
78,976 KB |
testcase_51 | AC | 47 ms
53,632 KB |
testcase_52 | AC | 134 ms
77,184 KB |
ソースコード
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)