Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14336 Accepted Submission(s): 5688
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 ?
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.
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 2
31).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
Author
Teddy
Source
Recommend
lcy
01背包問題,這種背包特點是:每種物品僅有一件,可以選擇放或不放。
用子問題定義狀態:即dp[i][v]表示前i件物品恰放入一個容量為v的背包可以獲得的最大價值。
則其狀態轉移方程便是:
dp[i][v]=max{dp[i-1][v],dp[i-1][v-cost[i]]+value[i]}
用子問題定義狀態:即dp[i][v]表示前i件物品恰放入一個容量為v的背包可以獲得的最大價值。
則其狀態轉移方程便是:
dp[i][v]=max{dp[i-1][v],dp[i-1][v-cost[i]]+value[i]}
注意體積為零的情況,如:
1
5 0
2 4 1 5 1
0 0 1 0 0
結果為12
#include<iostream> using namespace std; int dp[1000][1000]; int max(int x,int y) { return x>y?x:y; } int main() { int t,n,v,i,j; int va[1000],vo[1000]; cin>>t; while(t--) { cin>>n>>v; for(i=1;i<=n;i++) cin>>va[i]; for(i=1;i<=n;i++) cin>>vo[i]; memset(dp,0,sizeof(dp));//初始化操作 for(i=1;i<=n;i++) { for(j=0;j<=v;j++) { if(vo[i]<=j)//表示第i個物品將放入大小為j的背包中 dp[i][j]=max(dp[i-1][j],dp[i-1][j-vo[i]]+va[i]);//第i個物品放入后,那么前i-1個物品可能會放入也可能因為剩余空間不夠無法放入 else //第i個物品無法放入 dp[i][j]=dp[i-1][j]; } } cout<<dp[n][v]<<endl; } return 0; }
該題的第二種解法就是對背包的優化解法,當然只能對空間就行優化,時間是不能優化的。
先考慮上面講的基本思路如何實現,肯定是有一個主循環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,不能反過來,如果反過來就會造成物品重復放置!
#include<iostream> using namespace std; #define Size 1111 int va[Size],vo[Size]; int dp[Size]; int Max(int x,int y) { return x>y?x:y; } int main() { int t,n,v; int i,j; cin>>t; while(t--) { cin>>n>>v; for(i=1;i<=n;i++) cin>>va[i]; for(i=1;i<=n;i++) cin>>vo[i]; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) { for(j=v;j>=vo[i];j--) { dp[j]=Max(dp[j],dp[j-vo[i]]+va[i]); } } cout<<dp[v]<<endl; } return 0; }