假如我们使用回溯算法解决该问题, 代码如下 intmaxW=0;//最大重量intn=5;//物品数目intw=9;// 背包最大重量intweight[]={2,2,4,6,3};// 物品重量,2,2,4,6,3voidrucksack(inti,intcw){if(cw==w||i==n){if(cw>maxW)maxW=w;return;}rucksack(i+1,cw);//不装第i个物品if(cw+weight[...
intsunw=0,sumv=0,maxValue=0; inttotalWeight=0,totalValue=0,position,state; for(state=0;state<COUNT;state++) { package.state=state; totalWeight=package.flags.good_1_flag*weight[0]+package.flags.good_2_flag*weight[1] +package.flags.good_3_flag*weight[2]+package.flags.good_4_flag*we...
intsunw=0,sumv=0,maxValue=0; inttotalWeight=0,totalValue=0,position,state; for(state=0;state<COUNT;state++) { package.state=state; totalWeight=package.flags.good_1_flag*weight[0]+package.flags.good_2_flag*weight[1] +package.flags.good_3_flag*weight[2]+package.flags.good_4_flag*we...
总的来说,就是先随便装k件物品,再贪心往里装。取最优解。需测的子集数目为 O(n^k),每一个子集所需时间为 O(n),加上前期排序时间O(nlgn),因此当 k>0时,总的时间开销为O(n^(k+1))。http://read.pudn.com/downloads169/ebook/781908/01bag%20problem%20greedy.pdf ...
蛮力法解决 0_1背包问题新思路 -——利用 C语言位域类型 废话不说了,直接上代码 #include<stdio.h> #include<math.h> #define N 5 //物品种类数目 #define CAPACITY 6 //背包容量 #define COUNT 32 int weight[N]={3,2,1,4,5}; int value[N]={25,20,15,40,50}; union{ unsigned char state...