- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
回溯法-0-1背包问题(Backtracking -0-1 knapsack problem)
回溯法-0-1背包问题(Backtracking -0-1 knapsack problem)
Source code:
1. without a pruning function, very similar to the loading problem
#includeiostream
#includealgorithm
Using namespace std;
Class Knapsack{
Public:
Knapsack (double, *pp, double, *ww, int, NN, double, CC) {
P = pp;
W = ww;
N = nn;
C = cc;
CW = 0;
CP = 0;
Bestp = 0;
X = new int[n];
CX = new int[n];
}
Void, knapsack () {
Backtrack (0);
}
Void backtrack (int i) {// backtracking
If (I n) {
If (CP bestp) {
Bestp = cp;
For (int i = 0; I n; i++)
X[i] = cx[i];
}
Return;
}
If (CW + w[i] = C) {// right subtree search
CW = w[i];
CP = p[i];
Cx[i] = 1;
Backtrack (i+1);
CW = w[i];
CP = p[i];
}
Cx[i] = 0;
Backtrack (i+1); / / search the left subtree
}
Void, printResult () {
Cout the maximum value that can be loaded is: bestp endl;
Cout the contents of the order are as follows.:;
For (int i = 0; I n; i++) {
If (x[i] = 1)
Cout i+1;
}
Cout endl;
}
Private:
Double, *p, *w;
Int n;
Double c;
Double bestp, CP, CW; / / the maximum value, current value, current weight.
Int, *x, *cx;
};
Int, main () {
Double p[4] = {9,10,7,4}, w[4] = {3,5,2,1};
Knapsack KS = Knapsack (P, W, 4,7);
Ks.knapsack ();
Ks.printResult ();
Return 0;
}
2. recursive backtracking with pruning functions (to be seen)
#includeiostream
Using namespace std;
Class Knap
{
Friend, int, Knapsack (int, p[], int, w[], int, C, int, n);
Public:
Void, print ()
{
For (int m=1; m=n; m++)
{
Coutbestx[m];
}
Coutendl;
};
Private:
Int Bound (int i);
Void Backtrack (int i);
Int c; / / Backpack Capacity
Int n; / / the number of items
Int *w; / / weight array
Int *p; / / array value items
Int CW; / / current weight.
Int CP; / / current value
Int bestp; / / the current optimal value
Int *bestx; / / the current optimal solution
Int *x; / / the current solution
};
Int Knap:: Bound (int, I)
{
/ / upper bound calculation
Int cleft=c-cw; / / residual capacity
Int b=cp;
In order to decrease goods / goods value per unit weight
While (i=nw[i]=cleft)
{
Cleft-=w[i];
B+=p[i];
I++;
}
/ / lo
您可能关注的文档
- 古埃及、古希腊及古罗马神话入门简介(A brief introduction to myths of ancient Egypt, ancient Greece and Rome).doc
- 古文-冷泉亭记(The ancient pavilion - Cold Spring).doc
- 古时称谓(Ancient appellation).doc
- 古汉语同义词辨析的途径_以_制_度_为例(_ way of ancient Chinese synonyms in _ system _ _ degree as an example).doc
- 古罗马建筑特色赏析(Appreciation of architectural features in ancient Rome).doc
- 古诗词常用字词、意象(Commonly used words and images in ancient poetry).doc
- 古龙群侠传全攻略(Energy-saving.).doc
- 古风诗词发展的轨迹(The development track of ancient poetry).doc
- 古龙群侠传港服升级表和挂狗秘籍(Energy-saving port service upgrade and hang dog.).doc
- 古诗词满分解密(Full decryption of Ancient Poems).doc
- 回眸历史记载的赵氏孤儿(Review of historical records of the orphan).doc
- 回到作为理想和信念的家园(Return to the homeland of ideals and beliefs).doc
- 回采巷道冲击地压事故分析与防治措施管理(Analysis and prevention measures of rock burst accident in mining tunnel).doc
- 固定资产管理制度(Fixed assets management system).doc
- 国企税负系私管理企5倍 垄断业薪酬增幅低于均值(State owned tax burden system 5 times private management, monopoly pay growth is lower than the average).doc
- 国债和国债市场(Treasury bonds and treasury bonds market).doc
- 国内外120个摄影网站+50个设计网站(Domestic and foreign 120 photography website, +50 design website).doc
- 国外劳动力流动与教育互动及其启示_以英国_美国和德国为例(Foreign labor mobility and interactive education and Its Enlightenment _ in Britain, the United States and Germany for example _).doc
- 国内长途区号表(Domestic toll code).doc
- 国内·国际机场三字代码(Domestic and international airport three word code).doc
最近下载
- 口服国产托伐普坦片治疗肝硬化低钠血症的疗效观察.doc VIP
- 工程制图习题集(第3版)钟宏民 课后习题答案解析.pdf
- (青岛2024版)初中信息技术第一册 2.1 制作电子班牌 课件.pptx
- 新华师大版7年级上册数学全册课件(2024年秋新版教材).pptx
- 太阳能光热发电站安装调试技术.docx
- 手册三菱q系列plc培训.pdf
- 富士达各功能电梯地址码(1)(1).pdf VIP
- 10S505 柔性接口给水管道支墩.pptx VIP
- 序篇 不忘初心(第一课时)课件-2024-2025学年高中音乐人音版(2019)必修 音乐鉴赏.pptx VIP
- 2025年人教版三年级上册道德与法治精编知识点(新教材).pdf
文档评论(0)