HDU 2602 01背包的思考

来源:互联网 发布:淘宝推广计划怎么写 编辑:程序博客网 时间:2024/06/06 04:25

Bone Collector

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


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
 

/********非AC代码*******如果对记忆化搜索不是很熟练的话,可能会把前面的搜索写成这样目前选择的物品价值总和是 sum,从第i个物品之后的物品中挑选重量总和小于j的物品注意:在需要剪枝的情况下,可能会像这样把各种参数都写在函数上,但是在这种情况下会让记忆化搜索难以实现,需要注意。*/#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n, v;int val[1010];int vol[1010];int maxVal(int i, int cur, int sum){    int ans;    if(i == n)        ans = sum;    else if(cur < vol[i])        ans = maxVal(i+1, cur, sum);    else        ans = max(maxVal(i+1, cur, sum), maxVal(i+1, cur-vol[i], sum+val[i]));    return ans;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&v);        for(int i = 0; i < n; i++)            scanf("%d",&val[i]);        for(int i = 0; i < n; i++)            scanf("%d",&vol[i]);        printf("%d\n",maxVal(0, v, 0));    }    return 0;}

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n, v;int val[1010];int vol[1010];int opt[1010][1010];///适合改为记忆化搜索的形式int maxVal(int i, int cur){    int ans;    if(i == n)        ans = 0;    else if(cur < vol[i])        ans = maxVal(i+1, cur);    else        ans = max(maxVal(i+1, cur), maxVal(i+1, cur-vol[i])+val[i]);    return ans;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&v);        for(int i = 0; i < n; i++)            scanf("%d",&val[i]);        for(int i = 0; i < n; i++)            scanf("%d",&vol[i]);        memset(opt, -1, sizeof(opt));        printf("%d\n",maxVal(0,v));    }    return 0;}

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n, v;int val[1010];int vol[1010];int opt[1010][1010];///记忆化搜索int maxVal(int i, int cur){    int ans;    if(opt[i][cur] != -1)        return opt[i][cur];    if(i == n)        ans = 0;    else if(cur < vol[i])        ans = maxVal(i+1, cur);    else        ans = max(maxVal(i+1, cur), maxVal(i+1, cur-vol[i])+val[i]);    return opt[i][cur] = ans;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&v);        for(int i = 0; i < n; i++)            scanf("%d",&val[i]);        for(int i = 0; i < n; i++)            scanf("%d",&vol[i]);        memset(opt, -1, sizeof(opt));        printf("%d\n",maxVal(0,v));    }    return 0;}

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int val[1010];int vol[1010];int dp[1010][1010];int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n,v;        scanf("%d%d",&n,&v);        for(int i = 1; i <= n; i++)            scanf("%d",&val[i]);        for(int i = 1; i <= n; i++)            scanf("%d",&vol[i]);        memset(dp,0,sizeof(dp));        for(int i = 1; i <= n; i++)///DP        {            for(int j = v; j >= 0; j--)            {                if(vol[i]<=j)                    dp[i][j] = max(dp[i-1][j],dp[i-1][j-vol[i]]+val[i]);                else                    dp[i][j] = dp[i-1][j];            }        }        printf("%d\n",dp[n][v]);    }    return 0;}

0 0
原创粉丝点击