結果
問題 | No.2565 はじめてのおつかい |
ユーザー | Theta |
提出日時 | 2023-12-15 16:04:10 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 600 ms / 2,000 ms |
コード長 | 1,426 bytes |
コンパイル時間 | 166 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 49,664 KB |
最終ジャッジ日時 | 2024-09-27 06:28:34 |
合計ジャッジ時間 | 16,986 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 34 ms
11,520 KB |
testcase_01 | AC | 34 ms
11,648 KB |
testcase_02 | AC | 35 ms
11,776 KB |
testcase_03 | AC | 600 ms
49,664 KB |
testcase_04 | AC | 491 ms
49,664 KB |
testcase_05 | AC | 35 ms
11,520 KB |
testcase_06 | AC | 340 ms
23,040 KB |
testcase_07 | AC | 201 ms
17,664 KB |
testcase_08 | AC | 185 ms
17,920 KB |
testcase_09 | AC | 315 ms
21,760 KB |
testcase_10 | AC | 223 ms
18,944 KB |
testcase_11 | AC | 450 ms
30,848 KB |
testcase_12 | AC | 160 ms
15,616 KB |
testcase_13 | AC | 226 ms
18,304 KB |
testcase_14 | AC | 328 ms
24,704 KB |
testcase_15 | AC | 349 ms
23,808 KB |
testcase_16 | AC | 359 ms
37,632 KB |
testcase_17 | AC | 101 ms
15,360 KB |
testcase_18 | AC | 126 ms
22,400 KB |
testcase_19 | AC | 403 ms
28,480 KB |
testcase_20 | AC | 360 ms
27,008 KB |
testcase_21 | AC | 337 ms
27,136 KB |
testcase_22 | AC | 207 ms
22,016 KB |
testcase_23 | AC | 230 ms
21,760 KB |
testcase_24 | AC | 67 ms
13,696 KB |
testcase_25 | AC | 365 ms
27,392 KB |
testcase_26 | AC | 238 ms
20,992 KB |
testcase_27 | AC | 343 ms
28,648 KB |
testcase_28 | AC | 401 ms
27,904 KB |
testcase_29 | AC | 434 ms
32,640 KB |
testcase_30 | AC | 367 ms
30,464 KB |
testcase_31 | AC | 359 ms
27,696 KB |
testcase_32 | AC | 225 ms
21,248 KB |
testcase_33 | AC | 367 ms
29,312 KB |
testcase_34 | AC | 385 ms
25,856 KB |
testcase_35 | AC | 361 ms
33,536 KB |
testcase_36 | AC | 379 ms
29,320 KB |
testcase_37 | AC | 233 ms
21,504 KB |
testcase_38 | AC | 365 ms
25,984 KB |
testcase_39 | AC | 312 ms
23,168 KB |
testcase_40 | AC | 427 ms
32,512 KB |
testcase_41 | AC | 434 ms
34,280 KB |
testcase_42 | AC | 243 ms
20,352 KB |
testcase_43 | AC | 338 ms
24,832 KB |
testcase_44 | AC | 193 ms
18,048 KB |
testcase_45 | AC | 117 ms
15,872 KB |
testcase_46 | AC | 370 ms
24,320 KB |
testcase_47 | AC | 287 ms
20,608 KB |
testcase_48 | AC | 218 ms
18,560 KB |
testcase_49 | AC | 151 ms
15,232 KB |
testcase_50 | AC | 351 ms
20,480 KB |
testcase_51 | AC | 34 ms
11,520 KB |
testcase_52 | AC | 340 ms
22,272 KB |
ソースコード
from collections import deque from math import inf, isinf from typing import Iterable import sys def printe(*args, end="\n"): print(*args, end=end, file=sys.stderr) def distance_bfs(graph: list[Iterable[int]], dep: int) -> list[int | float]: distance = [inf for _ in graph] distance[dep] = 0 queue = deque() queue.append(dep) while queue: current = queue.popleft() for neighbor in graph[current]: if distance[neighbor] > distance[current] + 1: distance[neighbor] = distance[current] + 1 queue.append(neighbor) return distance def main(): N, M = map(int, input().split()) graph: list[set[int]] = [set() for _ in range(N)] for _ in range(M): dep, dest = map(lambda n: int(n) - 1, input().split()) graph[dep].add(dest) from_1_distance = distance_bfs(graph, 0) from_N_distance = distance_bfs(graph, N - 1) from_N_1_distance = distance_bfs(graph, N - 2) # 先にN-1を通る場合 distance_cand = from_1_distance[N - 2] + \ from_N_1_distance[N - 1] + from_N_distance[0] # 先にNを通る場合 distance_cand2 = from_1_distance[N - 1] + \ from_N_distance[N - 2] + from_N_1_distance[0] min_distance = min(distance_cand, distance_cand2) if isinf(min_distance): print(-1) else: print(min_distance) if __name__ == "__main__": main()