切原木问题(动态规划法)

切原木问题

题目

给定一根长度为N米的原木;另有一个分段价格表,给出长度L1,L2,…Li,…Lk米所对应的价格P1,P2…Pk(Li,Pi均为正整数),求切割原木分段出售所能获得的最大收益。 例如,根据下面给出的价格表,

Li	1	2	3	4	5	6	7	8	9	10Pi	1	5	8	9	10	17	17	20	23	28

若要出售一段8米长的原木,最优解是将其切割为2米和6米的两段,这样可以获得最大收益=L2+L6=5+17=22。而若要出售一段3米长的原木,最优解是根本不要切割,直接售出。

输入格式:
首行输入N,k,紧接着第二行为k个Li(递增有序)和第三行对应的k个Pi值。 (0<N,k<1000) 。

输出格式:
对应原木的最大切割收益(题目中保证最大收益值在int范围)。

输入样例:
在这里给出一组输入。例如:

8 10
1 2 3 4 5  6  7  8  9  10
1 5 8 9 10 17 17 20 23 28

输出样例:
在这里给出相应的输出。例如:

22

答案

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int num,n;
	cin>>num>>n;
	int len[n],cost[n],dp[n];
	fill(dp,dp+n,0);
	for(int i=0;i<n;i++)
	cin>>len[i];
	for(int i=0;i<n;i++)
	cin>>cost[i];
	for(int i=0;i<n;i++)
	{
		for(int j=len[i];j<=num;j++)
		dp[j]=max(dp[j],dp[j-len[i]]+cost[i]);
	}
	cout<<dp[num];
}

参考

切原木问题 (25分) (完全背包)

material_specs = { 1: { 'length': 5.5, 'defects': [(1.0, 0.03), (2.5, 0.04)], 'cost': 18 }, 2: { 'length': 6.2, 'defects': [(0.5, 0.02), (1.8, 0.05)], 'cost': 22 }, 3: { 'length': 7.8, 'defects': [(3.0, 0.03)], 'cost': 28 } } def calculate_metrics(used_materials): """计算切割损失率和利用率""" total_raw_length = 0.0 total_pieces_length = 0.0 total_defects = 0.0 total_saw_kerf = 0.0 total_remaining = 0.0 for usage in used_materials: mat_type = usage['material_type'] specs = material_specs[mat_type] # 计算缺陷总长度 defect_total = sum(d[1] for d in specs['defects']) # 获取切割参数 cuts = usage['cuts'] num_cuts = len(cuts) pieces_total = sum(cuts) if num_cuts > 0: saw_kerf = (num_cuts - 1) * 0.005 # 锯口总损耗 else: saw_kerf = 0# 计算有效可用长度(考虑缺陷位置) defect_intervals = [(start, start + length) for start, length in specs['defects']] available_lengths = [] prev_end = 0 for start, end in defect_intervals: if prev_end < start: available_lengths.append(start - prev_end) prev_end = end if prev_end < specs['length']: available_lengths.append(specs['length'] - prev_end) effective_length = sum(available_lengths) # 计算剩余材料 consumed = pieces_total + saw_kerf if consumed <= effective_length: remaining = effective_length - consumed else: remaining = 0 pieces_total = effective_length - saw_kerf # 修正切割部件总长度,避免超可用长度 # 累加统计量 total_raw_length += specs['length'] total_pieces_length += pieces_total total_defects += defect_total total_saw_kerf += saw_kerf total_remaining += remaining # 计算指标 total_loss = total_defects + total_saw_kerf + total_remaining loss_rate = (total_loss / total_raw_length) * 100 if total_raw_length != 0 else 0 utilization = (total_pieces_length / total_raw_length) * 100 if total_raw_length != 0 else 0 return loss_rate, utilization # 示例使用(需要实际切割方案数据) sample_usage = [ { 'material_type': 1, 'cuts': [1.6, 1.6, 2.2, 2.2] # 假设切割4个部件 }, { 'material_type': 2, 'cuts': [1.8, 1.8, 2.4, 2.4] } ] loss_rate, utilization = calculate_metrics(sample_usage) print(f"切割损失率: {loss_rate:.2f}%") print(f"材料利用率: {utilization:.2f}%")
05-11
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值