1.beibao.h文件代码如下:
#ifndef BEIBAO_H
#define BEIBAO_H
#include <math.h>
//子空间中节点类型
class BBnode{
public:
BBnode* parent; //父节点
bool leftChild; //左儿子节点标志
BBnode(BBnode* par,bool ch){
parent=par;
leftChild=ch;
}
BBnode(){
}
};
class HeapNode {
public:
BBnode* liveNode; // 活结点
double upperProfit; //结点的价值上界
double profit; //结点所相应的价值
double weight; //结点所相应的重量
int level; // 活结点在子集树中所处的层次号
//构造方法
HeapNode(BBnode* node, double up, double pp , double ww,int lev){
liveNode = node;
upperProfit = up;
profit = pp;
weight = ww;
level = lev;
}
HeapNode(){
}
int compareTo(HeapNode o) {
double xup =o.upperProfit;
if(upperProfit < xup)
return -1;
if(upperProfit == xup)
return 0;
else
return 1;
}
};
class Element {
public:
int id;
double d;
Element(){
}
Element(int idd,double dd){
id=idd;
d=dd;
}
int compareTo(Element x){
double xd=x.d;
if(d<xd)return -1;
if(d==xd)return 0;
return 1;
}
bool equals(Element x){
return d==x.d;
}
};
class MaxHeap{
public:
HeapNode *nodes;
int nextPlace;
int maxNumber;
MaxHeap(int n){
maxNumber = pow((double)2,(double)n);
nextPlace = 1;//下一个存放位置
nodes = new HeapNode[maxNumber];
}
MaxHeap(){
}
void put(HeapNode node){
nodes[nextPlace] = node;
nextPlace++;
heapSort(nodes);
}
HeapNode removeMax(){
HeapNode tempNode = nodes[1];
nextPlace--;
nodes[1] = nodes[nextPlace];
heapSort(nodes);
return tempNode;
}
void heapAdjust(HeapNode * nodes,int s,int m){
HeapNode rc = nodes[s];
for(int j=2*s;j<=m;j*=2){
if(j<m&&nodes[j].upperProfit<nodes[j+1].upperProfit)
++j;
if(!(rc.upperProfit<nodes[j].upperProfit))
break;
nodes[s] = nodes[j];
s = j;
}
nodes[s] = rc;
}
void heapSort(HeapNode * nodes){
for(int i=(nextPlace-1)/2;i>0;--i){
heapAdjust(nodes,i,nextPlace-1);
}
}
} ;
#endif
2.测试代码
#include <iostream>
using namespace std;
//子空间中节点类型
#include "beibao.h"
double c=30;
const int n=3;
double *w;
double *p;
double cw;
double cp;
int *bestX;
MaxHeap * heap;
//上界函数bound计算结点所相应价值的上界
double bound(int i){
double cleft=c-cw;
double b=cp;
while(i<=n&&w[i]<=cleft){
cleft=cleft-w[i];
b=b+p[i];
i++;
}
//装填剩余容量装满背包
if(i<=n)
b=b+p[i]/w[i]*cleft;
return b;
}
//addLiveNode将一个新的活结点插入到子集树和优先队列中
void addLiveNode(double up,double pp,double ww,int lev,BBnode* par,bool ch){
//将一个新的活结点插入到子集树和最大堆中
BBnode *b=new BBnode(par,ch);
HeapNode node =HeapNode(b,up,pp,ww,lev);
heap->put(node);
}
double MaxKnapsack(){
//优先队列式分支限界法,返回最大价值,bestx返回最优解
BBnode * enode=new BBnode();
int i=1;
double bestp=0;//当前最优值
double up=bound(1);//当前上界
while(i!=n+1){//非叶子结点
//检查当前扩展结点的左儿子子结点
double wt=cw+w[i];
if(wt<=c){
if(cp+p[i]>bestp)
bestp=cp+p[i];
addLiveNode(up,cp+p[i],cw+w[i],i+1,enode,true);
}
up=bound(i+1);
if(up>=bestp)
addLiveNode(up,cp,cw,i+1,enode,false);
HeapNode node =heap->removeMax();
enode=node.liveNode;
cw=node.weight;
cp=node.profit;
up=node.upperProfit;
i=node.level;
}
for(int j=n;j>0;j--){
bestX[j]=(enode->leftChild)?1:0;
enode=enode->parent;
}
return cp;
}
double knapsack(double *pp,double *ww,double cc,int *xx){
//返回最大值,bestX返回最优解
c=cc;
//n=sizeof(pp)/sizeof(double);
//定义以单位重量价值排序的物品数组
Element *q=new Element[n];
double ws=0.0;
double ps=0.0;
for(int i=0;i<n;i++){
q[i]=Element(i+1,pp[i+1]/ww[i+1]);
ps=ps+pp[i+1];
ws=ws+ww[i+1];
}
if(ws<=c){
return ps;
}
p=new double[n+1];
w=new double[n+1];
for(int i=0;i<n;i++){
p[i+1]=pp[q[i].id];
w[i+1]=ww[q[i].id];
}
cw=0.0;
cp=0.0;
bestX = new int[n+1];
heap = new MaxHeap(n);
double bestp = MaxKnapsack();
for(int j=0;j<n;j++)
xx[q[j].id]=bestX[j+1];
return bestp;
}
void main(){
w=new double[4];
w[1]=16;w[2]=15;w[3]=15;
p=new double[4];
p[1]=45;p[2]=25;p[3]=25;
int *x = new int[4];
double m = knapsack(p,w,c,x);
cout<<"*****分支限界法*****"<<endl;
cout<<"*****物品个数:n="<<n<<endl;
cout<<"*****背包容量:c="<<c<<endl;
cout<<"*****物品重量数组:w= {"<<w[3]<<" "<<w[1]<<" "<<w[2]<<"}"<<endl;
cout<<"*****物品价值数组:v= {"<<p[3]<<" "<<p[1]<<" "<<p[2]<<"}"<<endl;
cout<<"*****最优值:="<<m<<endl;
cout<<"*****选中的物品是:";
for(int i=1;i<=3;i++)
cout<<x[i]<<" ";
cout<<endl;
}
*****分支限界法*****
*****物品个数:n=3
*****背包容量:c=30
*****物品重量数组:w= {15 16 15}
*****物品价值数组:v= {25 45 25}
*****最优值:=50
*****选中的物品是:0 1 1
Charles 官网说明 https://www.charlesproxy.com/documentation/using-charles/ssl-certificates/AndroidAs of Android N, you need to add configuration to your app in order to have it trust the SSL certificates ...
????????关注后回复“进群”,拉你进程序员交流群????????英文:https://arpitbhayani.me/blogs/string-interning作者:arpit...
.CN,Internet网络域名,国家顶级域名,表示中国国家域名。它由我国国际互联网络信息中心(Inter NIC)正式注册并运行。.CN域名是全球唯一由中国管理的英文国际顶级域名,是中国企业自己的互联网标识,它体现了一种文化的认同、自身的价值和定位。2016年11月11日,中国互联网络信息中心公布最新统计数据显示,中国国家顶级域名“.CN”的注册保有量成功跨越2000万大关。发展历程199...
RS485总线由于其布线简单,稳定可靠从而广泛的应用于视频监控,门禁对讲,楼宇报警等各个领域中,但是,在485总线布线过程中由于有很多不完全准确的概念导致出现很多问题。现在将一些错误的观念作出一些澄清。1. 485信号线可以和强电电源线一同走线。在实际施工当中,由于走线都是通过管线走的,施工方有的时候为了图方便,直接将485信号线和电源线绑在一起,由...
//CSSinput[type=&quot;date&quot;]:before{ color:#A9A9A9;content:attr(placeholder);}input[type=&quot;date&quot;].full:before { color:black;content:&quot;&quot;!important;}//HTML&amp;lt;input type=&quot
oracle 通过sqlLoader导入含有json大字段数据的.ctl配置文件写法
【问题描述】天空中有一些星星,这些星星都在不同的位置,每个星星有个坐标。如果一个星星的左下方(包含正左和正下)有k颗星星,就说这颗星星是k级的。【编程任务】给定星星的位置,输出各级星星的数目。 给定n个点,定义每个点的等级是在该点左下方(含相等)的点的数目,试统计每个等级有多少个点。(n<=15000,0<=x,y<=32000)思路:树状数组,设一个ansansans记录当前价值有多少个点,用树状数组每次做一次累加代码:#include<iostream>
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本...
这是因为22H2的所有功能都已经隐藏在最新的累积更新中,并且版本(v2004)之后发布的功能更新基本上共享相同的操作系统核心版本。根据为操作系统发布的最新可选更新(KB5015684)中发现的参考资料,这可能是该操作系统的下一个功能更新,并将在几个月内开始向消费者推出。微软官员此前曾表示,启用包更新将仅附带“范围内的一组功能”,此类更新类似于每月累积更新,这实质上意味着Windows1022H2将在今年晚些时候通过轻弹开关启用。此更新旨在恢复对拖放的支持,改进开始菜单自定义设置等。...
/****************************************************checkBox复选按钮********************************************************//*常规状态*/QCheckBox{ color: #b1b1b1; /*字体颜色*/ spacing: 2px; /*文本与指示器的间隔*/}/*选中*/QCheckBox:chec
连接数据开始使用 Tableau。第一步是加载一些数据。 如果尚未启动 Tableau 的话,请先启动。你应该看到如下所示的界面(如果版本是 10): 你将在左侧边栏中看到可以连接的数据源。对于文件资源,你可以连接到 Excel 文件、文本文件(例如 CSV)或统计文件(例如来自 SAS、SPSS 和 R 的文件)。你可以使用 OData 连接到一些远程资源(连接到服务器),还可以...
排序算法,基本的高级语言都有一些提供。C语言有qsort()函数,C++有sort()函数,java语言有Arrays类(不是Array)。用这些排序时,都可以写自己的排序规则。Java API对Arrays类的说明是:此类包含用来操作数组(比如排序和搜索)的各种方法。