欢迎来到天天文库
浏览记录
ID:20459988
大小:129.86 KB
页数:20页
时间:2018-10-10
《北邮算法设计--背包问题实验报告》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、一.实验小组成员:二.实验内容:利用动态规划算法解决0-1背问题:要求1:算出最优解,给出最优组合。实现基本算法,调试成功;实现改进算法,调试成功。三.算法实现a)动态规划算法://不放物品时,价值都力0;for(intzero=1;zero2、[weightth]=f[object-1][weightth];//记录当前背包里的重S及价值,即第object个物品还未装下一个物//品时的背包的价值等于第object-1个物品装入后背包的价值if(weightth>=w[object]&&f[object-1][weightth-w[object]]+v[object]>f[object][weightth])f[object][weightth]=f[object-l][weightth-w[object]]+v[object];}//产生最佳方案for(obj3、ect=number;object>0;-object){if(ffobject][capability!!=ffobject-11[capability])//不相等吋,可知第object个物品放入了背包{path.push(wfobjectl);//添加最优解capability-=w[object];}}b)改进算法:voidclear(li$t&self){list::iteratorp=self.begin();list::iteratore=self.end()4、;list::iteratorn=p;++n;while(n!=e)f(p->second>=n->second)self.erase(n);n=p;++n;}else{++P;++n;}}}voidsort(list&selflist,list&end)"将selflist和other合并为end{1ist::iteratorsl=selflist.begin();list:iteratorel=sel5、flist.end();list:iterators2=other.begin();list::iteratore2=other.end();while(sl!=el&&s2!=e2)intwin(2);if(sl->firstfirstfirst)win=1;if(s2-〉first〉capability)win=0;if(l==win){end.push_back(*s1);++sl;}elseif(2==win){end.push_back6、(*s2);++s2;}else{++sl;++s2;}}if(sl==el)while(s2!=e2)if(s2->first<=capability)end.push_back(*s2);++s2;}}else{while(sl!=el){if(sl->first::iterators=self.begin();list::it7、eratore=self.end();-e;while(e!=s&&e->first>=w)if(e-〉first==w&&e-〉second==v)return1;-e;}if(e->first==w&&e->second==v)return1;return0;}一.实验程序1、”o-r背包问题的动态规划#defineMAXN100//最人物品数量#defineMAXV500//最大容量intfTMAXN+lirMAXV+11;intw[MAXN+1],v[MAXN+1];//重量和价值#include8、m>#includeusingnamespacestd;voidmain()intweight;intvalue;intnumber;intcapability;stackpath;cout«”请输入物品总数以及背包容量”<
2、[weightth]=f[object-1][weightth];//记录当前背包里的重S及价值,即第object个物品还未装下一个物//品时的背包的价值等于第object-1个物品装入后背包的价值if(weightth>=w[object]&&f[object-1][weightth-w[object]]+v[object]>f[object][weightth])f[object][weightth]=f[object-l][weightth-w[object]]+v[object];}//产生最佳方案for(obj
3、ect=number;object>0;-object){if(ffobject][capability!!=ffobject-11[capability])//不相等吋,可知第object个物品放入了背包{path.push(wfobjectl);//添加最优解capability-=w[object];}}b)改进算法:voidclear(li$t&self){list::iteratorp=self.begin();list::iteratore=self.end()
4、;list::iteratorn=p;++n;while(n!=e)f(p->second>=n->second)self.erase(n);n=p;++n;}else{++P;++n;}}}voidsort(list&selflist,list&end)"将selflist和other合并为end{1ist::iteratorsl=selflist.begin();list:iteratorel=sel
5、flist.end();list:iterators2=other.begin();list::iteratore2=other.end();while(sl!=el&&s2!=e2)intwin(2);if(sl->firstfirstfirst)win=1;if(s2-〉first〉capability)win=0;if(l==win){end.push_back(*s1);++sl;}elseif(2==win){end.push_back
6、(*s2);++s2;}else{++sl;++s2;}}if(sl==el)while(s2!=e2)if(s2->first<=capability)end.push_back(*s2);++s2;}}else{while(sl!=el){if(sl->first::iterators=self.begin();list::it
7、eratore=self.end();-e;while(e!=s&&e->first>=w)if(e-〉first==w&&e-〉second==v)return1;-e;}if(e->first==w&&e->second==v)return1;return0;}一.实验程序1、”o-r背包问题的动态规划#defineMAXN100//最人物品数量#defineMAXV500//最大容量intfTMAXN+lirMAXV+11;intw[MAXN+1],v[MAXN+1];//重量和价值#include8、m>#includeusingnamespacestd;voidmain()intweight;intvalue;intnumber;intcapability;stackpath;cout«”请输入物品总数以及背包容量”<
8、m>#includeusingnamespacestd;voidmain()intweight;intvalue;intnumber;intcapability;stackpath;cout«”请输入物品总数以及背包容量”<
此文档下载收益归作者所有