HUD2602 Bone Collector(01背包)

来源:互联网 发布:天互数据备案 编辑:程序博客网 时间:2024/06/08 18:47

【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=2602

题目意思

题目大意:给你n种价值的东西,没个物品都有个价值m和体积v,问给定一个V的背包存最大的价值。

解题思路

标准的01背包题,没坑直接上代码了

代码部分

#include <bits/stdc++.h>using namespace std;#define LL long longconst int maxn=1005;const int INF=0x3f3f3f;int v[maxn];   ///物品体积int m[maxn];   ///物品价值LL dp[maxn];   ///背包int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,temp;        memset(dp,0,sizeof(dp));        scanf("%d %d",&n,&temp);        for (int i=0;i<n;i++)        {            scanf("%d",&m[i]);        }        for (int i=0;i<n;i++)        {            scanf("%d",&v[i]);        }        for (int i=0;i<n;i++)   ///n种物品        {            for (int j=temp;j>=v[i];j--)   ///背包从大到小跟新            {                if (dp[j]<dp[j-v[i]]+m[i])                {                    dp[j]=dp[j-v[i]]+m[i];                }            }        }        printf("%d\n",dp[temp]);    }}