P1133 装箱问题

描述

有一个箱子容量为v(正整数,o≤v≤20000),同时有n个物品(o≤n≤30),每个物品有一个体积 (正整数)。要求从 n 个物品中,任取若千个装入箱内,使箱子的剩余空间为最小。

格式

输入格式

第一行,一个整数,表示箱子容量;
第二行,一个整数,表示有n个物品;
接下来n行,分别表示这n个物品的各自体积。

输出格式

一个整数,表示箱子剩余空间。

样例1

样例输入1

24
6
8
3
12
7
9
7

 
 

样例输出1

0

 
 

限制

每个测试点1s

来源

noip2001普及组第四题

分析:背包问题,将物品的重量和价值看成一样即可,最终输出剩余空间,即总的空间减去所能装入最大空间即可。

#include <iostream>
#include <memory.h>
using namespace std;
struct wupin
{
    int v;//体积
    int w;//价值
}a[10000];
int dp[100000];
int main()
{
    int n,m;
    cin>>m>>n;
    memset(dp,0,sizeof(dp));
    memset(a,0,sizeof(a));
    for(int i=0;i<n;i++)
        {
            cin>>a[i].v;
            a[i].w=a[i].v;
        }
    for(int i=0;i<n;i++)
        for(int j=m;j>=a[i].v;j--)
            dp[j]=max(dp[j],dp[j-a[i].v]+a[i].w);
    cout << dp[m] << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
基于退火算法求解三维装箱问题的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个物品,求解,输出最优解的体积及装箱方案的详细信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎曼猜想·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值