結果
問題 | No.2565 はじめてのおつかい |
ユーザー | oginoshikibu |
提出日時 | 2023-12-02 15:51:40 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 252 ms / 2,000 ms |
コード長 | 1,415 bytes |
コンパイル時間 | 381 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 113,544 KB |
最終ジャッジ日時 | 2024-09-26 19:25:13 |
合計ジャッジ時間 | 9,868 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
54,144 KB |
testcase_01 | AC | 39 ms
53,888 KB |
testcase_02 | AC | 40 ms
53,888 KB |
testcase_03 | AC | 229 ms
113,544 KB |
testcase_04 | AC | 213 ms
113,280 KB |
testcase_05 | AC | 40 ms
54,272 KB |
testcase_06 | AC | 187 ms
83,840 KB |
testcase_07 | AC | 133 ms
79,708 KB |
testcase_08 | AC | 163 ms
83,420 KB |
testcase_09 | AC | 162 ms
82,048 KB |
testcase_10 | AC | 171 ms
84,352 KB |
testcase_11 | AC | 252 ms
97,152 KB |
testcase_12 | AC | 113 ms
78,720 KB |
testcase_13 | AC | 161 ms
82,944 KB |
testcase_14 | AC | 208 ms
90,076 KB |
testcase_15 | AC | 202 ms
88,192 KB |
testcase_16 | AC | 143 ms
94,208 KB |
testcase_17 | AC | 84 ms
77,888 KB |
testcase_18 | AC | 84 ms
79,712 KB |
testcase_19 | AC | 179 ms
93,568 KB |
testcase_20 | AC | 170 ms
92,032 KB |
testcase_21 | AC | 179 ms
93,184 KB |
testcase_22 | AC | 129 ms
84,352 KB |
testcase_23 | AC | 126 ms
84,608 KB |
testcase_24 | AC | 76 ms
77,568 KB |
testcase_25 | AC | 166 ms
92,032 KB |
testcase_26 | AC | 154 ms
85,576 KB |
testcase_27 | AC | 138 ms
89,640 KB |
testcase_28 | AC | 185 ms
92,928 KB |
testcase_29 | AC | 238 ms
99,840 KB |
testcase_30 | AC | 149 ms
91,904 KB |
testcase_31 | AC | 193 ms
93,184 KB |
testcase_32 | AC | 111 ms
83,456 KB |
testcase_33 | AC | 150 ms
90,748 KB |
testcase_34 | AC | 171 ms
89,344 KB |
testcase_35 | AC | 148 ms
92,476 KB |
testcase_36 | AC | 175 ms
93,468 KB |
testcase_37 | AC | 154 ms
86,272 KB |
testcase_38 | AC | 146 ms
88,064 KB |
testcase_39 | AC | 209 ms
88,448 KB |
testcase_40 | AC | 162 ms
95,232 KB |
testcase_41 | AC | 188 ms
96,456 KB |
testcase_42 | AC | 154 ms
85,504 KB |
testcase_43 | AC | 130 ms
87,296 KB |
testcase_44 | AC | 118 ms
82,560 KB |
testcase_45 | AC | 83 ms
77,972 KB |
testcase_46 | AC | 225 ms
89,856 KB |
testcase_47 | AC | 147 ms
81,116 KB |
testcase_48 | AC | 158 ms
83,320 KB |
testcase_49 | AC | 110 ms
78,464 KB |
testcase_50 | AC | 162 ms
81,804 KB |
testcase_51 | AC | 38 ms
53,760 KB |
testcase_52 | AC | 121 ms
79,304 KB |
ソースコード
from collections import defaultdict, deque n, m = map(int, input().split()) graph = defaultdict(set) for i in range(m): a, b = map(lambda x: int(x) - 1, input().split()) graph[a].add(b) INF = float("inf") startdist = [INF for _ in range(n)] startdist[0]=0 startD=deque() startD.append([0,0]) while startD: now, dist = startD.popleft() if startdist[now] < dist: continue for next in graph[now]: if startdist[next] > dist + 1: startdist[next] = dist + 1 startD.append([next, dist + 1]) if startdist[-1]==INF or startdist[-2]==INF: print("-1") exit() INF = float("inf") n1dist = [INF for _ in range(n)] n1dist[-1]=0 n1D=deque() n1D.append([n-1,0]) while n1D: now, dist = n1D.popleft() if n1dist[now] < dist: continue for next in graph[now]: if n1dist[next] > dist + 1: n1dist[next] = dist + 1 n1D.append([next, dist + 1]) INF = float("inf") n2dist = [INF for _ in range(n)] n2dist[-2]=0 n2D=deque() n2D.append([n-2,0]) while n2D: now, dist = n2D.popleft() if n2dist[now] < dist: continue for next in graph[now]: if n2dist[next] > dist + 1: n2dist[next] = dist + 1 n2D.append([next, dist + 1]) ans = INF ans = min(ans, startdist[-1]+n1dist[-2]+n2dist[0]) ans = min(ans, startdist[-2]+n2dist[-1]+n1dist[0]) print(ans if ans!=INF else -1)