HDU-2844 Coins (多重背包)

多重背包问题的优化算法
本文探讨了多重背包问题,并提出了一种优化算法。通过使用二进制优化和01背包算法,确保了每个背包恰好装满。文章分享了一个详细的C++代码实现,解决了在已知硬币面值和数量的情况下,计算出可以支付的价格数量。

题目:

Whuacmers use coins.They have coins of value A1,A2,A3…An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn’t know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3…An and C1,C2,C3…Cn corresponding to the number of Tony’s coins of value A1,A2,A3…An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input

The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3…An,C1,C2,C3…Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4


  • 多重背包问题,要求每个背包恰好装满,因此把dp[0]赋为0,其他赋值负无穷。
  • 一开始忘记二进制优化最后要把剩余的数量再做一次01背包,因此老是漏掉2*1的解。
  • 觉得自己把所有东西都写在一块太难看了,因此借鉴了kuangbin大神的模板,瞬间清爽多了。

This is Code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define M(a, b) memset(a, b, sizeof(a))
 4 #define INF -0x3f3f3f3f
 5 int val[100], num[100], dp[100005], n, m;
 6 
 7 void ZeroOnePack(int cost, int weight){
 8     for (int i = m; i >= cost; i--)
 9         dp[i] = max(dp[i-cost]+weight, dp[i]);
10 }
11 
12 void CompletePack(int cost, int weight){
13     for (int i = cost; i <= m; i++)
14         dp[i] = max(dp[i-cost]+weight, dp[i]);
15 }
16 
17 void MultiplePack(int cost, int weight, int amount){
18     if (cost*amount >= m) CompletePack(cost, weight);
19     else{
20         int k = 1;
21         while(k < amount){
22             ZeroOnePack(k*cost, k*weight);
23             amount -= k;
24             k <<= 1;
25         }
26         ZeroOnePack(amount*cost, amount*weight);//不要漏解
27     }
28 }
29 
30 int main()
31 {
32     while(scanf("%d%d", &n, &m), n || m){
33         M(val, 0);
34         M(num, 0);
35         for (int i = 1; i <= m; i++) dp[i] = INF;
36         dp[0] = 0;
37         int maxn = 0;
38         for (int i = 0; i < n; i++) scanf("%d", &val[i]);
39         for (int i = 0; i < n; i++){
40             scanf("%d", &num[i]);
41             maxn += val[i] * num[i];
42         }
43         m = min(maxn, m);//背包容量取预计最大金额和所有硬币最大金额的最小值
44         for (int i = 0; i < n; i++){
45             MultiplePack(val[i], val[i], num[i]);
46         }
47         int ans = 0;
48         for (int i = 0; i <= m ; i++)
49             if(dp[i] > 0) ++ans;
50 
51         printf("%d\n", ans);
52     }
53 
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/robin1998/p/6359134.html

内容概要:本文介绍了一个基于多传感器融合的定位系统设计方案,采用GPS、里程计和电子罗盘作为定位传感器,利用扩展卡尔曼滤波(EKF)算法对多源传感器数据进行融合处理,最终输出目标的滤波后位置信息,并提供了完整的Matlab代码实现。该方法有效提升了定位精度与稳定性,尤其适用于存在单一传感器误差或信号丢失的复杂环境,如自动驾驶、移动采用GPS、里程计和电子罗盘作为定位传感器,EKF作为多传感器的融合算法,最终输出目标的滤波位置(Matlab代码实现)机器人导航等领域。文中详细阐述了各传感器的数据建模方式、状态转移与观测方程构建,以及EKF算法的具体实现步骤,具有较强的工程实践价值。; 适合人群:具备一定Matlab编程基础,熟悉传感器原理和滤波算法的高校研究生、科研人员及从事自动驾驶、机器人导航等相关领域的工程技术人员。; 使用场景及目标:①学习和掌握多传感器融合的基本理论与实现方法;②应用于移动机器人、无人车、无人机等系统的高精度定位与导航开发;③作为EKF算法在实际工程中应用的教学案例或项目参考; 阅读建议:建议读者结合Matlab代码逐行理解算法实现过程,重点关注状态预测与观测更新模块的设计逻辑,可尝试引入真实传感器数据或仿真噪声环境以验证算法鲁棒性,并进一步拓展至UKF、PF等更高级滤波算法的研究与对比。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值