动态规划算法据序偶原理求解0/1背包问题(C++实现)
1//说明:本程序有一定代码冗余,若分割为多个函数的形式会使程序简洁明了。
2#include <iostream>
3using namespace std;
4#include <queue>
5#include <stack>
6
7const int N=5; //物品个数,其中包括(0,0).
8const float W=60; //背包最大载重。
9struct GoodsStruct
10{
11 int ID; //物品编号
12 float benefit; //物品效益
13 float weight; //物品重量
14 int com; //标记最优解中是否含有该物品,有则为1,没有则为0.
15};
16
17int main()
18{
19 GoodsStruct StructGoods;
20 GoodsStruct StructTemp;
21 GoodsStruct StructCom;
22 GoodsStruct StructCalTemp;
23 queue <GoodsStruct> QueueGoods; //存放原始物品信息的队列,用于求解集合,完后全部被弹出。
24 queue <GoodsStruct> QueueSourse;//存放原始物品信息的队列,用于回溯过程,作为最后结果。
25 queue <GoodsStruct> QueueTemp;
26 queue <GoodsStruct> QueuePush;
27 queue <GoodsStruct> QueueCom; //两个队列在支配规则下的合并结果队列。
28 queue <GoodsStruct> QueueComTemp;//两个队列在支配规则下的合并时的临时变量。
29 queue <GoodsStruct> QueueCalTemp;
30 stack <queue<GoodsStruct>> StackGoods;
31
32 /**//*float benefits[N]={0,2,2,1};//测试数据。
33 float weights[N]={0,2,3,2};*/
34 /**//*float benefits[N]={0,2,3,4};
35 float weights[N]={0,1,2,5};*/
36 float benefits[N]={0,2,5,8,1};
37 float weights[N]={0,10,15,6,9};
38 bool check=false;
39
40 //将物品信息存入队列和堆栈中。
41 for (int i=0;i<N;i++)
42 {
43 StructTemp.ID=i;
44 StructTemp.benefit=benefits[i];
45 StructTemp.weight=weights[i];
46 StructTemp.com=0;
47 QueueSourse.push(StructTemp);
48 }
49
50 /**///
51 //求解集合
52 //(0,0)先入栈。
53 QueueGoods=QueueSourse;
54 StructGoods=QueueGoods.front();
55 QueueGoods.pop();
56 QueuePush.push(StructGoods);
57 StackGoods.push(QueuePush);
58
59 while (!QueueGoods.empty())
60 {
61 StructGoods=QueueGoods.front();
62 QueueGoods.pop();
63 QueueTemp=QueuePush;
64 //计算出临时队列的过程,此处使用支配规则。
65 while (!QueueTemp.empty())
66 {
67 StructTemp=QueueTemp.front();
68 QueueTemp.pop();
69 StructTemp.benefit+=StructGoods.benefit;
70 StructTemp.weight+=StructGoods.weight;
71
72 QueueCalTemp=QueuePush;
73 if (StructTemp.weight<=W)
74 {
75 while (!QueueCalTemp.empty())
76 {
77 StructCalTemp=QueueCalTemp.front();
78 QueueCalTemp.pop();
79 if (StructTemp.weight>=StructCalTemp.weight && StructTemp.benefit<StructCalTemp.benefit)
80 {
81 //支配规则把新值StructCom去掉。
82 check=false;
83 break;
84 }
85 else
86 {
87 check=true;
88 }
89 }
90 if (check==true)
91 {
92 QueueComTemp.push(StructTemp);
93 }
94 }
95 }
96
97 //QueueComTemp与QueuePush通过支配规则进行合并,去掉不可能的解。
98 while (!QueuePush.empty())
99 {
100 StructCom=QueuePush.front();
101 QueuePush.pop();
102 QueueTemp=QueueComTemp;
103 while (!QueueTemp.empty())
104 {
105 StructTemp=QueueTemp.front();
106 QueueTemp.pop();
107 if (StructCom.weight>=StructTemp.weight && StructCom.benefit<StructTemp.benefit)
108 {
109 //支配规则把新值StructCom去掉。
110 check=false;
111 break;
112 }
113 else
114 {
115 check=true;
116 }
117 }
118 if (check==true)
119 {
120 QueueCom.push(StructCom);
121 }
122 }
123 //将QueueComTemp全部接入QueueCom中,因为QueueComTemp不会被支配掉。
124 while (!QueueComTemp.empty())
125 {
126 StructTemp=QueueComTemp.front();
127 QueueComTemp.pop();
128 QueueCom.push(StructTemp);
129 }
130 StackGoods.push(QueueCom);
131 QueuePush=QueueCom;
132 while (!QueueCom.empty())
133 {
134 QueueCom.pop();
135 }
136 }
137
138 /**///
139 //求解最终序偶
140 QueueTemp=StackGoods.top();
141 StructGoods=QueueTemp.back();
142 while (!QueueTemp.empty())
143 {
144 StructTemp=QueueTemp.front();
145 QueueTemp.pop();
146 if (StructGoods.weight<StructTemp.weight)
147 {
148 StructGoods=StructTemp;
149 }
150 }
151 cout<<"(P,W)=("<<StructGoods.benefit<<","<<StructGoods.weight<<")"<<endl;
152
153 /**///
154 //回溯
155 int count=N-1;
156 StackGoods.pop();
157 while(!StackGoods.empty())
158 {
159 QueueTemp=StackGoods.top();
160 StackGoods.pop();
161 while (!QueueTemp.empty())
162 {
163 StructTemp=QueueTemp.front();
164 QueueTemp.pop();
165 if (StructTemp.benefit==StructGoods.benefit && StructTemp.weight==StructGoods.weight)
166 {
167 check=false;
168 for (int i=0;i<N;i++)
169 {
170 StructCalTemp=QueueSourse.front();
171 QueueSourse.pop();
172 if (StructCalTemp.ID==count)
173 {
174 StructCalTemp.com=0;
175 }
176 QueueSourse.push(StructCalTemp);
177 }
178 break;
179 }
180 else
181 {
182 check=true;
183 }
184 }
185 if (check==true)
186 {
187 for (int i=0;i<N;i++)
188 {
189 StructCalTemp=QueueSourse.front();
190 QueueSourse.pop();
191 if (StructCalTemp.ID==count)
192 {
193 StructCalTemp.com=1;
194 }
195 QueueSourse.push(StructCalTemp);
196 }
197 StructGoods.benefit-=StructCalTemp.benefit;
198 StructGoods.weight-=StructCalTemp.weight;
199 }
200 count--;
201 }
202
203 QueueSourse.pop();
204 while(!QueueSourse.empty())
205 {
206 StructTemp=QueueSourse.front();
207 QueueSourse.pop();
208 cout<<"X"<<count+1<<"="<<StructTemp.com<<endl;
209 count++;
210 }
211 return 0;
212}
转载于:https://www.cnblogs.com/fireice/archive/2009/05/02/1447928.html
动态规划算法据序偶原理求解0/1背包问题(C++实现)相关推荐
- 用滚动数组求解0/1背包问题
用滚动数组求解0/1背包问题(此处仅求装入背包的最大价值) // 由于第i个阶段(考虑物品i)的解dp[i][ * ]只与第i-1个阶段(考虑物品i-1)的解dp[i-1][ * ]有关,这种情况下保 ...
- 分枝限界法求解0/1背包问题
问题描述 有n个重量分别为{w1,w2,-,wn}的物品,它们的价值分别为{v1,v2,-,vn},给定一个容量为W的背包. 设计从这些物品中选取一部分物品放入该背包的方案,每个物品要么选中要么不选中 ...
- 动态规划法改进:用序偶法求0/1背包问题
动态规划法改进:用序偶法求0/1背包问题 1.问题 2.方法 3.实现代码 序偶法求0/1背包问题(动态规划法改进版) by 孙琨SealSun at UCAS 2015.11.20 #include ...
- 动态规划算法初步(6)——0/1 背包
动态规划算法初步(6) 例题五:0/1 背包(背包型) 题目: 一个旅行者有一个最多能装m公斤物品的背包,现在有n件物品,它们的重量分别是w1,w2,-,wn,它们的价值分别为c1,c2,-,cn.若 ...
- 动态规划算法(Dynamic Programming)之0-1背包问题
文章目录 1. 问题引入 2. 动态规划求解0-1背包 3. 复杂度 4. 0-1背包升级版(带价值) 5. 0-1背包升级版(带价值)DP解法 1. 问题引入 前面讲了0-1背包的回溯解决方法,它是 ...
- 动态规划法求解0/1背包问题
问题描述 有n个重量分别为{w1,w2,-,wn}的物品,它们的价值分别为{v1,v2,-,vn},给定一个容量为W的背包. 设计从这些物品中选取一部分物品放入该背包的方案,每个物品要么选中要么不选中 ...
- 回溯法 —— 求解0/1背包问题(剪枝)
0/1背包问题 题目描述: 有n个重量分别为w1,w2,-,wn的物品(物品编号为1~n),它们的价值分别为v1,v2,-,vn,给定一个容量为W的背包.设计从这些物品中选取一部分物品放入该背包的方案 ...
- 【武汉理工大学计算机复试刷题】(C语言)动态规划求解0/1背包问题之求最大价值
文章目录 题目描述 思路分析 代码 运行情况 输入文件 运行结果 发现的问题 题目描述 一个旅行者有一个最多能装M公斤的背包,现在有n件物品,它们的重星分别是W1,W2, - Wn,它们的价值分别为C ...
- tsp问题动态规划python_用Python解决TSP问题(2)——动态规划算法
本介绍用python解决TSP问题的第二个方法--动态规划法 算法介绍 动态规划算法根据的原理是,可以将原问题细分为规模更小的子问题,并且原问题的最优解中包含了子问题的最优解.也就是说,动态规划是一种 ...
最新文章
- 后端开发面试自我介绍_字节跳动暑期实习后端开发面试经历
- 清华团队曝光「新冠病毒」3D高清结构照!这个恶魔已感染1亿地球人
- R语言数值累加函数cumsum实战
- 某一列高度变化_高度近视,老了后会瞎吗?
- 用户控件和自定义控件
- java语言的数组描述_下列关于Java语言的数组描述中,错误的是()。_学小易找答案...
- 实变函数与泛函分析课本pdf_实变函数与泛函分析
- mysql show 存储过程_mysql 存储过程 show errors
- ApacheCN 人工智能知识树 v1.0
- ISO7816协议中APDU指令解析
- android 清空剪贴板,清空剪贴板app
- Latex自定义图表序号
- 在Linux部署SSM项目
- 详解 LVS、Nginx 及 HAProxy 工作原理
- 最新阿里云短信服务接口类【亲测成功】
- Redis基本使用|基本命令|redis事务|Jedis|持久化|订阅|集群|
- vb.net 换行符的转换
- 计算机win10内存,win10系统电脑可用内存异常的解决方法
- 2023年电工杯数学建模竞赛AB题思路代码论文资料汇总贴
- 花了10分钟,终于弄懂了特征值和特征向量到底有什么意义
热门文章
- SSH登陆警告问题known_hosts
- 数据库_触发器和事件
- 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标1077视频监控平台...
- FreeNAS-11.2-U7 smb共享和FTP设置
- Java日志体系日志门面(Slf4j)日志实现(Log4j、Log4j2)详解
- C/C++中函数声明的作用
- 【TLJH】the-littlest-jupyterhub国内搭建和配置详细教程
- C++复习day1:知识点概述(依据C++premier plus)
- windows系统win10使用curl命令模拟发送post/get请求
- 最新极光推送在ios模拟器上无法运行