P1049 装箱问题

题目描述

有一个箱子的容量为V,有N个物品,每个物品都有一个体积,要求在这N个物品中使箱子剩余的体积最小。

样例输入

24
6
8
3
12
7
9
7

样例输出

0

思路

O(nm)
连续写了4题关于动态规划的题目,但毫不例外的都是01背包,能不能再简单一点,我也是没看出有多少改变。
跟采药相比也就少了个每个物品的价值,但物品的体积也可以看作是它的价值,最后总体积减去最大体积就是答案,方程就变成了:
f[j]:=max(f[j],f[j-a[i]]+a[i]);
还是有点变化的啊~~~~
var
  n,m,i,j:longint;
  a,b,f:array[0..20000]of longint;
begin
  readln(n);
  readln(m);
  for i:=1 to m do
    readln(a[i]);
  for i:=1 to m do
    for j:=n downto a[i] do
      if f[j-a[i]]+a[i]>f[j] then
        f[j]:=f[j-a[i]]+a[i];
  writeln(n-f[n]);
end.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于退火算法求解三维装箱问题的Python代码可以如下: ```python import random import math # 定义装箱向量(Box)类 class Box: def __init__(self, x, y, z, id): self.x = x self.y = y self.z = z self.id = id # 生成n个待装箱的物品列表 def generate_items(n): items = [] for i in range(n): x = random.randint(1, 100) y = random.randint(1, 100) z = random.randint(1, 100) item = Box(x, y, z, i) items.append(item) return items # 计算方案的体积 def get_volume(boxes): volume = 0 for box in boxes: volume += box.x * box.y * box.z return volume # 计算温度 def get_temperature(iteration): return 200 / iteration # 判断是否接受新方案 def accept_new_solution(delta_E, T): if delta_E < 0: return True else: p = math.exp(-delta_E / T) if random.random() < p: return True else: return False # 利用退火算法求解三维装箱问题 def solve_packing(items, T0=200, alpha=0.95, stopping_temperature=10 ** -10, max_iteration=10 ** 4): current_solution = [] for item in items: current_solution.append(item) best_solution = current_solution current_volume = get_volume(current_solution) best_volume = current_volume T = T0 iteration = 0 while T > stopping_temperature and iteration < max_iteration: i = random.randint(0, len(items) - 1) j = random.randint(0, len(items) - 1) while j == i: j = random.randint(0, len(items) - 1) proposal_solution = current_solution.copy() proposal_solution[i], proposal_solution[j] = proposal_solution[j], proposal_solution[i] proposal_volume = get_volume(proposal_solution) delta_E = proposal_volume - current_volume if accept_new_solution(delta_E, T): current_solution = proposal_solution current_volume = proposal_volume if current_volume < best_volume: best_solution = current_solution best_volume = current_volume T = alpha * T iteration += 1 return best_volume, best_solution # 测试代码 if __name__ == '__main__': items = generate_items(10) # 生成10个待装箱的物品 volume, boxes = solve_packing(items) # 求解 print("体积:", volume) for box in boxes: print("物品{}:x={},y={},z={}".format(box.id, box.x, box.y, box.z)) ``` 在代码中,首先定义了表示装箱向量的类Box。generate_items函数用于生成n个待装箱的物品,在本例中生成了10个,每个物品的边长随机生成。get_volume函数用于计算方案的体积。get_temperature函数用于计算当前温度,其值会随着迭代次数不断降低。accept_new_solution函数用于根据温度随机判断是否接受新方案。solve_packing函数用于利用退火算法求解三维装箱问题,其中T0为初始温度,alpha为降温系数,stopping_temperature为停止温度,max_iteration为最大迭代次数。最后,测试代码生成10个物品,求解,输出最优解的体积及装箱方案的详细信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值