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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

hdu 2602 Bone Collector(01背包)模板

發布時間:2024/4/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 2602 Bone Collector(01背包)模板 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2602

Bone Collector

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 54132????Accepted Submission(s): 22670


Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

?

Input The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

?

Output One integer per line representing the maximum of the total value (this number will be less than 231).

?

Sample Input 1 5 10 1 2 3 4 5 5 4 3 2 1

?

Sample Output 14 01背包問題,這種背包特點是:每種物品僅有一件,可以選擇放或不放。
用子問題定義狀態:即dp[i][v]表示前i件物品恰放入一個容量為v的背包可以獲得的最大價值。
則其狀態轉移方程便是:
dp[i][v]=max{dp[i-1][v],dp[i-1][v-cost[i]]+value[i]} 1 #include<iostream> 2 using namespace std; 3 int dp[1000][1000]; 4 5 int max(int x,int y) 6 { 7 return x>y?x:y; 8 } 9 10 int main() 11 { 12 int t,n,v,i,j; 13 int va[1000],vo[1000]; 14 cin>>t; 15 while(t--) 16 { 17 cin>>n>>v; 18 for(i=1;i<=n;i++) 19 cin>>va[i]; 20 for(i=1;i<=n;i++) 21 cin>>vo[i]; 22 memset(dp,0,sizeof(dp));//初始化操作 23 for(i=1;i<=n;i++) 24 { 25 for(j=0;j<=v;j++) 26 { 27 if(vo[i]<=j)//表示第i個物品將放入大小為j的背包中 28 dp[i][j]=max(dp[i-1][j],dp[i-1][j-vo[i]]+va[i]);//第i個物品放入后,那么前i-1個物品可能會放入也可能因為剩余空間不夠無法放入 29 else //第i個物品無法放入 30 dp[i][j]=dp[i-1][j]; 31 } 32 } 33 cout<<dp[n][v]<<endl; 34 } 35 return 0; 36 }

?

該題的第二種解法就是對背包的優化解法,當然只能對空間就行優化,時間是不能優化的。

先考慮上面講的基本思路如何實現,肯定是有一個主循環i=1..N,每次算出來二維數組dp[i][0..V]的所有值。
那么,如果只用一個數組dp[0..V],能不能保證第i次循環結束后dp[v]中表示的就是我們定義的狀態dp[i][v]呢?

dp[i][v]是由dp[i-1][v]和dp[i-1][v-c[i]]兩個子問題遞推而來,能否保證在推dp[i][v]時(也即在第i次主循環中推dp[v]時)能夠得到dp[i-1][v]和dp[i-1][v-c[i]]的值呢?事實上,這要求在每次主循環中我們以v=V..0的順序推dp[v],這樣才能保證推dp[v]時dp[v-c[i]]保存的是狀態dp[i-1][v-c[i]]的值。偽代碼如下:

for i=1..N

??? for v=V..0

??????? dp[v]=max{dp[v],dp[v-c[i]]+w[i]};

注意:這種解法只能由V--0,不能反過來,如果反過來就會造成物品重復放置!

?

1 #include<iostream> 2 using namespace std; 3 #define Size 1111 4 int va[Size],vo[Size]; 5 int dp[Size]; 6 int Max(int x,int y) 7 { 8 return x>y?x:y; 9 } 10 int main() 11 { 12 int t,n,v; 13 int i,j; 14 cin>>t; 15 while(t--) 16 { 17 cin>>n>>v; 18 for(i=1;i<=n;i++) 19 cin>>va[i]; 20 for(i=1;i<=n;i++) 21 cin>>vo[i]; 22 memset(dp,0,sizeof(dp)); 23 for(i=1;i<=n;i++) 24 { 25 for(j=v;j>=vo[i];j--) 26 { 27 dp[j]=Max(dp[j],dp[j-vo[i]]+va[i]); 28 } 29 } 30 cout<<dp[v]<<endl; 31 } 32 return 0; 33 }

?

轉載于:https://www.cnblogs.com/yoke/p/6106079.html

總結

以上是生活随笔為你收集整理的hdu 2602 Bone Collector(01背包)模板的全部內容,希望文章能夠幫你解決所遇到的問題。

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