分支限界法求解背包问题Word文档格式.docx
- 文档编号:17381777
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:9
- 大小:61.94KB
分支限界法求解背包问题Word文档格式.docx
《分支限界法求解背包问题Word文档格式.docx》由会员分享,可在线阅读,更多相关《分支限界法求解背包问题Word文档格式.docx(9页珍藏版)》请在冰豆网上搜索。
/*背包的载重*/
intn;
/*物品的数量*/intweightarrayb[MAXSIZE]={0};
intvaluearrayb[MAXSIZE]={0};
floatcostarrayb[MAXSIZE]={0};
intfinalb[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"
value\n"
for(ii=1;
ii<
nn;
ii++)
no%d:
%-5d%-5d"
ii,weightarrayb[ii],valuearrayb[ii]);
\n"
/*把读取的数据按照性价比从大到小排序*/voidrangeb()
inti,j,k;
inttempvalue,tempweight,tempcost;
for(i=1;
i<
=n;
i++)
costarrayb[i]=valuearrayb[i]/weightarrayb[i];
for(j=1;
j<
j++)
for(k=j;
k<
k++)
if(costarrayb[j]<
costarrayb[k+1])
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;
value"
cost\n"
{
%-5d%-5d%-f"
i,weightarrayb[i],valuearrayb[i],costarrayb[i]);
/*分支限界法运算*/
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;
exemptyweightb[0]=BAGWEIGHT;
ub=tempvalue+emptyweight*costarrayb[1];
include0:
weight=0value=0ub=%f\n"
ub);
i=1;
while(weightarrayb[i]!
=0)
tempweight=tempweight+weightarrayb[i];
tempvalue=tempvalue+valuearrayb[i];
emptyweight=BAGWEIGHT-tempweight;
if(tempweight>
BAGWEIGHT)
include%d:
weight=%dcan'
t\n"
i,tempweight);
tempweight=extempweightb[i-1];
tempvalue=extempvalueb[i-1];
emptyweight=exemptyweightb[i-1];
ub=0;
ub=tempvalue+emptyweight*costarrayb[i+1];
weight=%dvalue=%dub=%f\n"
i,tempweight,tempvalue,ub);
extempweightb[i]=tempweight;
extempvalueb[i]=tempvalue;
exemptyweightb[i]=emptyweight;
exub=extempvalueb[i-1]+exemptyweightb[i-1]*costarrayb[i+1];
exclude%d:
weight=%dvalue=%d
ub=%f\n"
i,extempweightb[i-1],extempvalueb[i-1],exub);
if(ub<
exub||ub==0)
finalb[i]=2;
if(ub>
=exub)
finalb[i]=1;
thefinalansweris:
"
if(finalb[i]==1)
%d"
i);
finalweightb[u]=i;
u++;
elseif(finalb[i]==2)
continue;
=u;
allweight=allweight+weightarrayb[finalweightb[i]];
allvalue=allvalue+valuearrayb[finalweightb[i]];
thefinalweightis:
%d\n"
allweight);
thefinalvalueis:
%d\n"
allvalue);
/*自动生成文件*/
voidbecome()
inti;
w"
//srand(unsigned(time(NULL)));
for(i=0;
i<
n;
fprintf(fp,"
%d%d\n"
rand()%9+1,rand()%41+10);
/*删除文件*/
voiddel()
remove("
intmain()
请输入背包负重(大于0的整数):
"
scanf("
%d"
BAGWEIGHT);
请输入物品数量(大于0的整数):
n);
floattime_usea=0;
floattime_useb=0;
structtimevalstarta;
structtimevalenda;
structtimevalstartb;
structtimevalendb;
intk=0,i,j,u,loop;
loop=0;
gettimeofday(&
startb,NULL);
become();
readb();
100;
i++)/*由于分支限界太效率,时常显示0毫秒,为了能读出时间,让其运
行100次后总时间再除以100*/
rangeb();
branchb();
endb,NULL);
time_useb=(endb.tv_sec-startb.tv_sec)*1000+(endb.tv_usec-startb.tv_usec)/1000;
//毫秒
Ittookyou%f毫秒\n"
time_useb/100);
/*最后把算到的时间读出到文件*/
FILE*fp=NULL;
OUT.DAT"
"
a+"
if(NULL==fp)
wrong"
return0;
//fprintf(fp,"
蛮力:
%d%d%f\n"
BAGWEIGHT,n,time_usea);
fprintf(fp,"
分支:
BAGWEIGHT,n,time_useb/100);
del();
/*要运算多次,生成多次文件,所以每次算完删除文件*/
如果需要再算一次,请按1\n"
如果需要退出,请按2\n"
loop);
switch(loop)
case1:
returnmain();
case2:
return;
default:
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 分支 限界 求解 背包 问题