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
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背包問題,這種背包特點(diǎn)是:每種物品僅有一件,可以選擇放或不放。用子問題定義狀態(tài):即dp[i][v]表示前i件物品恰放入一個(gè)容量為v的背包可以獲得的最大價(jià)值。
則其狀態(tài)轉(zhuǎn)移方程便是:
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個(gè)物品將放入大小為j的背包中 28 dp[i][j]=max(dp[i-1][j],dp[i-1][j-vo[i]]+va[i]);//第i個(gè)物品放入后,那么前i-1個(gè)物品可能會(huì)放入也可能因?yàn)槭S嗫臻g不夠無法放入 29 else //第i個(gè)物品無法放入 30 dp[i][j]=dp[i-1][j]; 31 } 32 } 33 cout<<dp[n][v]<<endl; 34 } 35 return 0; 36 }
?
該題的第二種解法就是對(duì)背包的優(yōu)化解法,當(dāng)然只能對(duì)空間就行優(yōu)化,時(shí)間是不能優(yōu)化的。
先考慮上面講的基本思路如何實(shí)現(xiàn),肯定是有一個(gè)主循環(huán)i=1..N,每次算出來二維數(shù)組dp[i][0..V]的所有值。
那么,如果只用一個(gè)數(shù)組dp[0..V],能不能保證第i次循環(huán)結(jié)束后dp[v]中表示的就是我們定義的狀態(tài)dp[i][v]呢?
dp[i][v]是由dp[i-1][v]和dp[i-1][v-c[i]]兩個(gè)子問題遞推而來,能否保證在推dp[i][v]時(shí)(也即在第i次主循環(huán)中推dp[v]時(shí))能夠得到dp[i-1][v]和dp[i-1][v-c[i]]的值呢?事實(shí)上,這要求在每次主循環(huán)中我們以v=V..0的順序推dp[v],這樣才能保證推dp[v]時(shí)dp[v-c[i]]保存的是狀態(tà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,不能反過來,如果反過來就會(huì)造成物品重復(fù)放置!
?
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 }?
轉(zhuǎn)載于:https://www.cnblogs.com/yoke/p/6106079.html
總結(jié)
以上是生活随笔為你收集整理的hdu 2602 Bone Collector(01背包)模板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (Android第一行代码)活动的启动模
- 下一篇: 操作系统的实现(0)