首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

01双肩包,完全背包,多重背包 ,模板代码

2013-09-05 
01背包,完全背包,多重背包 ,模板代码01 背包 void bag01(int cost,int weight){ for(ivicosti--)if(d

01背包,完全背包,多重背包 ,模板代码

01 背包

 

void bag01(int cost,int weight){ for(i=v;i>=cost;i--)  if(dp[i]<dp[i-cost]+weight)   dp[i]=dp[i-cost]+weight;}


完全背包

 

void complete(int cost,int weight){ for(i=cost;i<=v;i++)  if(dp[i]<dp[i-cost]+weight)   dp[i]=dp[i-cost]+weight;}


 

多重背包

 

void multiply(int cost,int weight,int amount){ if(cost*amount>=v)  complete(cost,weight); else{  k=1;  while(k<amount){   bag01(k*cost,k*weight);   amount-=k;   k+=k;  }  bag01(cost*amount,weight*amount); }}


 

热点排行