杭电 2602 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
 

dp[i][v]=max{dp[i-1][v],dp[i-1][v-cost[i]]+value[i]}

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5 int f[1010][1010];
 6 int w[1010];
 7 int v[1010];
 8 int main()
 9 {
10     int t;
11     scanf("%d",&t);
12     while(t--)
13     {
14         int n,m,i,j;
15         scanf("%d %d",&n,&m);
16         for(i = 1 ; i <= n ; i++)
17         {
18             scanf("%d",&v[i]);
19         }
20         for(i = 1 ; i <= n ; i++)
21         {
22             scanf("%d",&w[i]);
23         }
24         memset(f,0,sizeof(f));
25         for(i = 1 ; i <= n ; i++)
26         {
27             for(j = 0 ; j <= m ; j++)
28             {
29                 f[i][j]=f[i-1][j];
30                 if(j >= w[i])
31                 {
32                     f[i][j]=max(f[i-1][j],f[i-1][j-w[i]]+v[i]);
33                 }
34             }
35         }
36         printf("%d
",f[n][m]);
37     }
38 }
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5 int f[1010];
 6 int w[1010];
 7 int v[1010];
 8 int main()
 9 {
10     int t;
11     scanf("%d",&t);
12     while(t--)
13     {
14         int n,m,i,j;
15         scanf("%d %d",&n,&m);
16         for(i = 1 ; i <= n ; i++)
17         {
18             scanf("%d",&v[i]);
19         }
20         for(i = 1 ; i <= n ; i++)
21         {
22             scanf("%d",&w[i]);
23         }
24         memset(f,0,sizeof(f));
25         for(i = 1 ; i <= n ; i++)
26         {
27             for(j = m ; j >= 0 ; j--)
28             {
29                 if(j >= w[i])
30                 {
31                     f[j]=max(f[j],f[j-w[i]]+v[i]);
32                 }
33             }
34         }
35         printf("%d
",f[m]);
36     }
37 }
原文地址:https://www.cnblogs.com/yexiaozi/p/5755737.html