分支限界法求解背包问题.docx
- 文档编号:2918565
- 上传时间:2022-11-16
- 格式:DOCX
- 页数:9
- 大小:34.60KB
分支限界法求解背包问题.docx
《分支限界法求解背包问题.docx》由会员分享,可在线阅读,更多相关《分支限界法求解背包问题.docx(9页珍藏版)》请在冰豆网上搜索。
分支限界法求解背包问题
分支限界法求解背包问题
/*此程序实现,分支限界法求解背包问题,分支限界法是根据上界=当前背包
的价值+背包
剩余载重*(剩余物品最大价值/质量)*/
thefinalanswerts:
1234thefinalweightts:
iethefinalvalue
weight
nol:
1
I
no2:
no3:
no5;no6;
Include
2
5
2
6
value
2斗
33
37
13
27
IQ
0:
Include:
exclude
1:
I:
Includeexclude
2:
2:
tnclddeexclude
1;
3;
itncludeexclude
4:
4:
includeexclude
5;
5:
Includeexclude
6;
6:
分支r10
I分S:
10
ts:
107cost
i6x0eoeeo
7-oeoeeo
M.se阴eo
i.eoeeoe
weights©
welght=lwelgbt=e
weight:
盂3welght=i
wet9ht=9welght=3
velue=»ub=24a.QQOeGO
value=24ub=i6S.dOddO6vaiue=3ub~i6e,Q9oeeG
value=57vaTue=24
va'Lue=57
ub=IC&.90Q@iQ6u!
b=87,000000
Ub=99-G00e00
weight=10value=i07ub=107.600660wetght-8v31ue=94ubiie&,O0e©06
Metght^lSwelght=10
welght=iSwelght=i0
41.200060'
62.i/eeoe
#include
#include
can't
value=i07ub=iei7.O0oee0
vaue=i07ub^iez.eoeeee
#include
#include
#include
#defineMAXSIZE20000
//#defineBAGWEIGHT200inta[MAXSIZE]={0};
intarray[MAXSIZE]={0};
intweightarray[MAXSIZE]={0};/*
存放各物品重量*/
intvaluearray[MAXSIZE]={0};/*
存放各物品价值*/
intlastweight[MAXSIZE]={0};intlastvalue[MAXSIZE]={0};intqq=0;
/*上面的数组,变量都是蛮力法所用到,下面的都是分支限界法所用到
*/
intBAGWEIGHT;/*背包的载重*/
intn;/*物品的数量*/intweightarrayb[MAXSIZE]={0};int
valuearrayb[MAXSIZE]={0};floatcostarrayb[MAXSIZE]={0};int
finalb[MAXSIZE]={0};
intfinalweightb[MAXSIZE]={0};
/*从文件读取数据*/
voidreadb()
intnn=1,ii=1;
inti=1;
FILE*fp;
fp=fopen("in.dat","rb");
while(!
feof(fp))
if(fscanf(fp,"%d%d",&weightarrayb[nn],&valuearrayb[nn])!
=EOF)
nn++;
i++;
else
break;
fclose(fp);
printf("weight");
printf("value\n");
for(ii=1;ii printf("no%d: %-5d%-5d",ii,weightarrayb[ii],valuearrayb[ii]); printf("\n"); /*把读取的数据按照性价比从大到小排序*/voidrangeb() inti,j,k; inttempvalue,tempweight,tempcost; for(i=1;i<=n;i++) costarrayb[i]=valuearrayb[i]/weightarrayb[i]; for(j=1;j<=n;j++) for(k=j;k<=n;k++) if(costarrayb[j] tempcost=costarrayb[j]; costarrayb[j]=costarrayb[k+1]; costarrayb[k+1]=tempcost; tempweight=weightarrayb[j]; weightarrayb[j]=weightarrayb[k+1]; weightarrayb[k+1]=tempweight; tempvalue=valuearrayb[j]; valuearrayb[j]=valuearrayb[k+1]; valuearrayb[k+1]=tempvalue; printf("value"); printf("cost\n"); for(i=1;i<=n;i++) printf("no%d: %-5d%-5d%-f",i,weightarrayb[i],valuearrayb[i],costarrayb[i]); printf("\n"); /*分支限界法运算*/ voidbranchb() inti,k,j,u=1; intemptyweight=BAGWEIGHT; inttempweight=0; inttempvalue=0; floatub; floatexub; intextempweightb[MAXSIZE]={0}; intextempvalueb[MAXSIZE]={0}; intexemptyweightb[MAXSIZE]={0}; intallweight=0; intallvalue=0; ub=tempvalue+emptyweight*costarrayb[1]; printf("include0: weight=0value=0ub=%f\n",ub); printf("\n"); i=1; while(weightarrayb[i]! =0) tempweight=tempweight+weightarrayb[i]; tempvalue=tempvalue+valuearrayb[i]; emptyweight=BAGWEIGHT-tempweight; if(tempweight>BAGWEIGHT) printf("include%d: weight=%dcan't\n",i,tempweight); tempweight=extempweightb[i-1]; tempvalue=extempvalueb[i-1]; emptyweight=exemptyweightb[i-1]; ub=0; else ub=tempvalue+emptyweight*costarrayb[i+1]; printf("include%d: weight=%dvalue=%dub=%f\n",i,tempweight,tempvalue,ub); extempvalueb[i]=tempvalue; exemptyweightb[i]=emptyweight; exub=extempvalueb[i-1]+exemptyweightb[i-1]*costarrayb[i+1]; printf("exclude%d: weight=%dvalue=%d ub=%f\n",i,extempweightb[i-1],extempvalueb[i-1],exub); printf("\n"); if(ub finalb[i]=2; if(ub>=exub) finalb[i]=1; i++; printf("thefinalansweris: "); for(i=1;i<=n;i++) if(finalb[i]==1) printf("%d",i); u++; elseif(finalb[i]==2) continue; printf("\n"); for(i=1;i<=u;i++) allweight=allweight+weightarrayb[finalweightb[i]]; allvalue=allvalue+valuearrayb[finalweightb[i]]; printf("thefinalweightis: %d\n",allweight); printf("thefinalvalueis: %d\n",allvalue); /*自动生成文件*/ voidbecome() inti; FILE*fp; fp=fopen("in.dat","w"); //srand(unsigned(time(NULL))); for(i=0;i fprintf(fp,"%d%d\n",rand()%9+1,rand()%41+10); fclose(fp); /*删除文件*/ voiddel() remove("in.dat"); intmain() printf("请输入背包负重(大于0的整数): "); scanf("%d",&BAGWEIGHT); printf("请输入物品数量(大于0的整数): "); scanf("%d",&n); floattime_usea=0; floattime_useb=0; structtimevalstarta; structtimevalenda; structtimevalstartb; structtimevalendb; intk=0,i,j,u,loop; loop=0; gettimeofday(&startb,NULL); become(); readb(); printf("\n"); for(i=0;i<100;i++)/* 由于分支限界太效率,时常显示0毫秒,为了能读出 时间,让其运 行100次后总时间再除以100*/ rangeb(); branchb(); gettimeofday(&endb,NULL); time_useb=(endb.tv_sec-startb.tv_sec)*1000+(endb.tv_usec- startb.tv_usec)/1000;//毫秒 del();/*要运算多次,生成多次文件,所以每次算完删除文件 */ /*
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 分支 限界 求解 背包 问题
![提示](https://static.bdocx.com/images/bang_tan.gif)