結果
問題 | No.2955 Pizza Delivery Plan |
ユーザー | LyricalMaestro |
提出日時 | 2024-11-24 19:24:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 273 ms / 2,000 ms |
コード長 | 2,493 bytes |
コンパイル時間 | 278 ms |
コンパイル使用メモリ | 82,404 KB |
実行使用メモリ | 86,232 KB |
最終ジャッジ日時 | 2024-11-24 19:24:40 |
合計ジャッジ時間 | 7,057 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
53,336 KB |
testcase_01 | AC | 36 ms
53,020 KB |
testcase_02 | AC | 37 ms
53,736 KB |
testcase_03 | AC | 36 ms
52,724 KB |
testcase_04 | AC | 36 ms
53,636 KB |
testcase_05 | AC | 36 ms
54,032 KB |
testcase_06 | AC | 36 ms
52,720 KB |
testcase_07 | AC | 36 ms
53,516 KB |
testcase_08 | AC | 137 ms
76,116 KB |
testcase_09 | AC | 162 ms
76,516 KB |
testcase_10 | AC | 172 ms
76,016 KB |
testcase_11 | AC | 205 ms
77,200 KB |
testcase_12 | AC | 228 ms
77,584 KB |
testcase_13 | AC | 170 ms
77,280 KB |
testcase_14 | AC | 186 ms
79,996 KB |
testcase_15 | AC | 225 ms
81,200 KB |
testcase_16 | AC | 222 ms
84,104 KB |
testcase_17 | AC | 240 ms
84,440 KB |
testcase_18 | AC | 253 ms
85,412 KB |
testcase_19 | AC | 247 ms
85,408 KB |
testcase_20 | AC | 273 ms
86,232 KB |
testcase_21 | AC | 246 ms
85,684 KB |
testcase_22 | AC | 244 ms
85,212 KB |
testcase_23 | AC | 240 ms
85,260 KB |
testcase_24 | AC | 272 ms
85,596 KB |
testcase_25 | AC | 193 ms
77,700 KB |
testcase_26 | AC | 198 ms
77,180 KB |
testcase_27 | AC | 171 ms
76,568 KB |
testcase_28 | AC | 246 ms
85,060 KB |
testcase_29 | AC | 249 ms
85,380 KB |
testcase_30 | AC | 250 ms
85,456 KB |
ソースコード
## https://yukicoder.me/problems/no/2955 import math def main(): N, K = map(int, input().split()) xy = [] for _ in range(N): x, y = map(int, input().split()) xy.append((x, y)) # 原点からの距離を計算 dists_from_origins = [] for x, y in xy: d = math.sqrt(x ** 2 + y ** 2) dists_from_origins.append(d) # 点i -> 点jのの移動距離の計算 matrix = [[-1] * N for _ in range(N)] for i in range(N): for j in range(N): x1, y1 = xy[i] x2, y2 = xy[j] d = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2 ) matrix[i][j] = d # popcountがK以下のbit(一度に宅配できる住宅の組み合わせ)ごとに最短でどれくらい帰ってくるかの計算を実施 base_bits = [[] for _ in range(K + 1)] for bit in range(2 ** N): popcount = 0 for i in range(N): if bit & (1 << i) > 0: popcount += 1 if popcount <= K: base_bits[popcount].append(bit) dp = {} for i in range(N): if (1 << i) not in dp: dp[1 << i] = {} dp[1 << i][i] = dists_from_origins[i] for k in range(1, K): b_bits = base_bits[k] for bit in b_bits: for j in range(N): if bit & (1 << j) > 0: continue new_bit = bit | (1 << j) for last, cost in dp[bit].items(): new_cost = cost + matrix[last][j] if new_bit not in dp: dp[new_bit] = {} if j not in dp[new_bit]: dp[new_bit][j] = float("inf") dp[new_bit][j] = min(dp[new_bit][j], new_cost) new_bit = {} for bit, bit_map in dp.items(): base_cost = float("inf") for key, cost in bit_map.items(): c = dists_from_origins[key] + cost base_cost = min(base_cost, c) new_bit[bit] = base_cost # --------- answer_dp = [float("inf")] * ( 2 ** N) answer_dp[0] = 0.0 for bit in range(1, 2 ** N): b = bit while b > 0: b_ = bit - b if b in new_bit: ans = new_bit[b] + answer_dp[b_] answer_dp[bit] = min(answer_dp[bit], ans) b = (b - 1) & bit print(answer_dp[2 ** N - 1]) if __name__ == "__main__": main()