乐逍遥xwl

导航

hdu 2602 (01背包)

Bone Collector

 

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

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<map>
 7 #include<vector>
 8 #include<set>
 9 using namespace std;
10 #define ll long long 
11 const int mod=1e9+7;
12 const int inf=1e9+7;
13 const int maxn=1005;
14 int dp[maxn][maxn];
15 int value[maxn];
16 int capacity[maxn];
17 int main()
18 {
19     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
20     int T;
21     cin>>T;
22     while(T--)
23     {
24         memset(dp,0,sizeof(dp));
25         int n,v;
26         cin>>n>>v;
27         for(int i=1;i<=n;i++)
28             cin>>value[i];
29         for(int i=1;i<=n;i++)
30             cin>>capacity[i];
31         for(int i=1;i<=n;i++)
32         {
33             for(int j=0;j<=v;j++)//注意体积为0时的情况 
34             {
35                 if(j<capacity[i])//放不进 
36                     dp[i][j]=dp[i-1][j];
37                 else
38                 {
39                     if(dp[i-1][j]>dp[i-1][j-capacity[i]]+value[i])//放得进但是亏了 
40                         dp[i][j]=dp[i-1][j];
41                     else
42                         dp[i][j]=dp[i-1][j-capacity[i]]+value[i];//不亏 
43                 }
44             }
45            }
46         cout<<dp[n][v]<<endl;  
47     }
48     return 0;
49 }

 

 

一维优化:

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<map>
 7 #include<vector>
 8 #include<set>
 9 using namespace std;
10 #define ll long long 
11 const int mod=1e9+7;
12 const int inf=1e9+7;
13 const int maxn=1005;
14 int dp[maxn];
15 int value[maxn];
16 int capacity[maxn];
17 int main()
18 {
19     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
20     int T;
21     cin>>T;
22     while(T--)
23     {
24         memset(dp,0,sizeof(dp));
25         int n,v;
26         cin>>n>>v;
27         for(int i=1;i<=n;i++)
28             cin>>value[i];
29         for(int i=1;i<=n;i++)
30             cin>>capacity[i];
31         for(int i=1;i<=n;i++)
32         {
33             for(int j=v;j>=capacity[i];j--)
34             {
35                 //if(j>=capacity[i])//由j的循环条件知,j一定大于等于capacity[i] 
36                 dp[j]=max(dp[j],dp[j-capacity[i]]+value[i]);
37             }
38         }
39         cout<<dp[v]<<endl;  
40     }
41     return 0;
42 }

 

 

 

posted on 2019-05-27 20:45  乐逍遥xwl  阅读(133)  评论(0编辑  收藏  举报