hdu 2602 Bone Collector

来源:互联网 发布:windows官方商城 编辑:程序博客网 时间:2024/05/29 18:24

Bone Collector

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


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
15 101 2 3 4 55 4 3 2 1
 

Sample Output
14
题目大意:
骨收集者有一个大袋子V,在他的收集之旅有很多骨头,显然,不同的骨骼具有不同的价值和不同的体积,现在给出他的旅行中的每个骨骼的价值,你可以计算骨收集器可以获得的总值的最大值?
输入:第一行包含整数T,个案数。其次是T个案,每个案例三行,第一行包含两个整数N,V,(N <= 1000,V <= 1000),代表骨数和他的行李的体积。第二行包含表示每个骨骼的值的N个整数。第三行包含表示每个骨的体积的N个整数。
输出:每行一个整数表示总值的最大值(该数字将小于2 31)。
题目解析:骨收藏者的大袋子就是一个背包,那么V就是背包容量,每个骨头都有相应的体积,求可以存放的最大价值,显然是一个01背包问题。
转移方程:
dp[j]=max(dp[j-volume[i]]+value[i],dp[j]);

代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define maxn 10005int dp[maxn];int i,j;int N,V,value[maxn],volume[maxn];int main(){    int T;    int i,j;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&N,&V);        for(i=1; i<=N; i++)            scanf("%d",&value[i]);        for(i=1; i<=N; i++)            scanf("%d",&volume[i]);             memset(dp,0,sizeof(dp));    for(i=1;i<=N;i++)        for(j=V;j>=volume[i];j--)        {            dp[j]=max(dp[j-volume[i]]+value[i],dp[j]);        }        printf("%d\n",dp[V]);    }    return 0;}





0 0
原创粉丝点击