#include<cstdio> int casenum; int boxnum; int boxw[22]; int boatload1,boatload2; int restw; int bestw=0; int noww; void dfs(int i){ // printf ( "% d of layer: bestw is:% d \ t values of noww:% d \ t remaining wt restw =% d \ n", i, bestw, noww, restw); if(i>boxnum){ if(noww>bestw){ bestw=noww; } } else{ Optimal solution if (noww + boxw [i] <= boatload1 && noww + restw> bestw) {// I just installed, the I and the rest of the box if all are installed before necessarily larger solution space I will not go! ENWW + = boxw [i]; if(noww>bestw) bestw=noww; restw- = boxw [i]; // this can be subtracted, which was amended in the rest of the weight to the next level when a recursive // printf ( "left subtree: bestw value% d \ t values of noww:% d \ t remaining wt restw =% d \ n", bestw, noww, restw); dfs(i+1); // printf ( "I've been \ n"); restw + = boxw [i]; // is recursive come back when I want to re-amend my weight value, but because of the weight of the following is not the i-th box so they do not add back, // wrong, or to be added back, because the i-th box when the road is likely to be cut, which would make the rest of the weight has not been subtracted // So you want to add yourself to go back in time following the second recursive just subtract yourself, so as Insurance noww- = boxw [i]; } restw- = boxw [i]; // Whether I installed or not installed, the i-th box, the rest of the i-th weight should lose weight boxes // printf ( "% d second intermediate layer @@@@@@@@@@@@@ layer: bestw is:% d t noww value of \:% d \ t remaining wt restw =% d \ n ", i, bestw, noww, restw); if (noww + restw> bestw) {// I do not install this i-th box, if coupled with the rest of the space is large enough solution, do not go // printf ( "I came second path \ n"); dfs(i+1); } restw + = boxw [i]; // finally have to re-add, because they do not increase, then the backtracking of time will cause restw been so small } // printf ( "% d of layer end ---------------------------- \ n", i, bestw); } int main () { scanf("%d",&casenum); for(int i=1;i<=casenum;++i){ scanf("%d",&boxnum); scanf("%d%d",&boatload1,&boatload2); bestw = 0; restw = 0; for(int j=1;j<=boxnum;++j){ scanf("%d",&boxw[j]); restw boxw + = [j]; } dfs(1); if(boatload2<restw-bestw){ printf("Case %d:\nNo\n",i); } else printf("Case %d:\n%d\n",i,bestw); } }
zhuangzai_wenti
Guess you like
Origin www.cnblogs.com/waibizi/p/12070210.html
Ranking