HDOJ 2602

Bone Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14134    Accepted Submission(s): 5585

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

View Code

 1 //下面的不对
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 typedef struct Node
 6 {
 7     int w,value;
 8 }Node;
 9 Node bag[1010];
10 int f[1010][1010];
11 int main()
12 {
13     int i,j,k,t,T;
14     cin>>T;
15     while(T--)
16     {
17         int n,c;
18         cin>>n>>c;
19         memset(bag,0,sizeof(bag));
20         memset(f,0,sizeof(f));
21         for(i=1;i<=n;i++)
22             cin>>bag[i].value;
23         for(i=1;i<=n;i++)
24             cin>>bag[i].w;
25         f[1][c] = bag[1].value;
26         for(i=2;i<=n;i++)
27         {
28             if(c>=bag[i].w)
29             {
30                 int temp = f[i-1][c-bag[i].w] + bag[i].value;
31                 f[i][c] = max(temp,f[i-1][c]);
32             }
33             else
34                 f[i][c] = f[i-1][c];
35         }
36         cout<<f[n][c]<<endl;
37     }
38     return 0;
39 }
40         
 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 int w[1010];
 5 int value[1010];
 6 int f[1010][1010];
 7 int main()
 8 {
 9     int i,j,k,t,T;
10     cin>>T;
11     while(T--)
12     {
13         int n,c;
14         cin>>n>>c;
15         memset(w,0,sizeof(w));
16         memset(f,0,sizeof(f));
17         memset(value,0,sizeof(value));
18         for(i=1;i<=n;i++)
19             cin>>value[i];
20         for(i=1;i<=n;i++)
21             cin>>w[i];
22         for(i=1;i<=n;i++)
23             for(j=0;j<=c;j++)
24             if(j>=w[i])
25             {
26                 f[i][j] = max(f[i-1][j],f[i-1][j-w[i]] + value[i]);
27             }
28             else
29                 f[i][j] = f[i-1][j];
30                 /*
31                 //原来没加这一条语句
32                 1
33                 5 0
34                 2 4 1 5 1
35                 0 0 1 0 0
36                 答案应该是12
37                 却输出6
38                 */
39             cout<<f[n][c]<<endl;
40     }
41     return 0;
42 }
43         

 

 1 //此时g++提交ac,c++wa,因为第二十九行的符号
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 int w[1010];
 6 int value[1010];
 7 int f[1010][1010];
 8 int main()
 9 {
10     int i,j,k,t,T;
11     cin>>T;
12     while(T--)
13     {
14         int n,c;
15         cin>>n>>c;
16         memset(w,0,sizeof(w));
17         memset(f,0,sizeof(f));
18         memset(value,0,sizeof(value));
19         for(i=1;i<=n;i++)
20             cin>>value[i];
21         for(i=1;i<=n;i++)
22             cin>>w[i];
23         for(i=1;i<=n;i++)
24             for(j=0;j<=c;j++)
25             {
26                 f[i][j] = f[i-1][j];
27                 if(j>=w[i])
28                 {
29                     f[i][j] >?= f[i-1][j-w[i]] + value[i];
30                 }
31             }
32             cout<<f[n][c]<<endl;
33     }
34     return 0;
35 }
36         

 

 1 //滚动数组优化空间和时间复杂度 ,时间上不太明显
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 int w[1010];
 6 int value[1010];
 7 int f[1010];
 8 int main()
 9 {
10     int i,j,k,t,T;
11     cin>>T;
12     while(T--)
13     {
14         int n,c;
15         cin>>n>>c;
16         memset(w,0,sizeof(w));
17         memset(f,0,sizeof(f));
18         memset(value,0,sizeof(value));
19         for(i=1;i<=n;i++)
20             cin>>value[i];
21         for(i=1;i<=n;i++)
22             cin>>w[i];
23         for(i=1;i<=n;i++)
24             for(j=c;j>=w[i];j--)
25             {
26                 f[j] >?= f[j-w[i]] + value[i];
27             }
28             cout<<f[c]<<endl;
29     }
30     return 0;
31 }
32 //注意一条:不可用结构体存储重量和价值      

 

时间: 2024-11-01 01:27:04

HDOJ 2602的相关文章

最大匹配-HDOJ 2458 Kindergarten

HDOJ 2458 Kindergarten Description In a kindergarten, there are a lot of kids. All girls of the kids know each other and all boys also know each other. In addition to that, some girls and boys know each other. Now the teachers want to pick some kids 

最大匹配-HDOJ 1068

HDOJ 1068 Girls and Boys . Problem Description the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study

hdu 2602 Bone Collector(0/1背包)

点击打开链接hdu 2602 思路: 裸0/1背包 代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N = 1010; const int MAXN = 1000010; int n , sum; int v[N] , w[N] , dp[N]; int solve(){ memset(dp

hdoj 1007 两点之间最短距离的二分之一 提交之后超时了 求c语言解法 感激不敬

问题描述 hdoj 1007 两点之间最短距离的二分之一 提交之后超时了 求c语言解法 感激不敬 #include"stdio.h" #include"math.h" int main() { int n,i,j; double s; while(scanf("%d",&n)&&n) { double a[100000][5]={0}; for(i=0;i<n;i++) for(j=0;j<2;j++) sca

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ 2057 A + B Again

Problem Description There must be many A + B problems in our HDOJ , now a new one is coming. Give you two hexadecimal integers , your task is to calculate the sum of them,and print it in hexadecimal too. Easy ? AC it ! Input The input contains severa

HDOJ 2033 人见人爱A+B

Problem Description HDOJ上面已经有10来道A+B的题目了,相信这些题目曾经是大家的最爱,希望今天的这个A+B能给大家带来好运,也希望这个题目能唤起大家对ACM曾经的热爱. 这个题目的A和B不是简单的整数,而是两个时间,A和B 都是由3个整数组成,分别表示时分秒,比如,假设A为34 45 56,就表示A所表示的时间是34小时 45分钟 56秒. Input 输入数据有多行组成,首先是一个整数N,表示测试实例的个数,然后是N行数据,每行有6个整数AH,AM,AS,BH,BM,

HDOJ 1001Sum Problem

Problem Description Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + - + n. Input The input will consist of a series of integers n, one integer per line. Output For each ca

HDOJ/HDU 2352 Verdis Quo(罗马数字与10进制数的转换)

Problem Description The Romans used letters from their Latin alphabet to represent each of the seven numerals in their number system. The list below shows which letters they used and what numeric value each of those letters represents: I = 1 V = 5 X