日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj 1276 多重背包

發布時間:2023/12/18 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 1276 多重背包 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
735 3 4 125 6 5 3 350    //735的最大額,3種,4個125,6個5,3個350 633 4 500 30 6 100 1 5 0 1 735 0 0 3 10 100 10 50 10 10

735 630 0 0 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 using namespace std; 8 const int maxn=100005; 9 int n,m,t; 10 int dp[maxn],w[maxn],v[maxn]; 11 int nValue; 12 //0-1背包,代價為cost,獲得的價值為weight 13 void ZeroOnePack(int cost,int weight) 14 { 15 for(int i=nValue;i>=cost;i--) 16 dp[i]=max(dp[i],dp[i-cost]+weight); 17 } 18 19 //完全背包,代價為cost,獲得的價值為weight 20 void CompletePack(int cost,int weight) 21 { 22 for(int i=cost;i<=nValue;i++) 23 dp[i]=max(dp[i],dp[i-cost]+weight); 24 } 25 26 //多重背包 27 void MultiplePack(int cost,int weight,int amount) 28 { 29 if(cost*amount>=nValue) CompletePack(cost,weight); 30 else 31 { 32 int k=1; 33 while(k<amount) 34 { 35 ZeroOnePack(k*cost,k*weight); 36 amount-=k; 37 k<<=1; 38 } 39 ZeroOnePack(amount*cost,amount*weight);//這個不要忘記了,經常掉了 40 } 41 } 42 int main() 43 { 44 int i,j,k; 45 #ifndef ONLINE_JUDGE 46 freopen("1.in","r",stdin); 47 #endif 48 while(scanf("%d%d",&nValue,&n)!=EOF) 49 { 50 for(i=0;i<n;i++) 51 { 52 scanf("%d%d",&w[i],&v[i]); 53 } 54 memset(dp,0,sizeof(dp)); 55 for(i=0;i<n;i++) 56 { 57 MultiplePack(v[i],v[i],w[i]); 58 } 59 printf("%d\n",dp[nValue]); 60 } 61 }

?

轉載于:https://www.cnblogs.com/cnblogs321114287/p/4356324.html

總結

以上是生活随笔為你收集整理的poj 1276 多重背包的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。