A - Bone Collector

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 ? 
01背包专题
 

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 2 31).
 

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1
 

Sample Output

14

 

>>题目链接<<

 

题意:给定背包容量,骨头的个数和每个骨头的价值,求在背包容量允许的情况下,最多装的价值。

思路:最基础的01背包,dp[j]=max(dp[j],dp[j-w[i]]+v[i]);

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    int N,V,T;

    int w[1005];
    int c[1005];
    int i,j,k;
    scanf("%d",&T);
    while(T--)
    {
        int f[1005]={0};
        scanf("%d%d",&N,&V);
        for(i=0;i<N;i++)
        {
            scanf("%d",&w[i]);
        }
        for(i=0;i<N;i++)
        {
            scanf("%d",&c[i]);
        }

        for(i=0;i<N;i++)
        {
  //          printf("\nc[%d]=%d\tw[%d]=%d\n",i,c[i],i,w[i]);
            for(int v=V;v>=c[i];v--)// 剩下的体积要大于想放入的体积
            {
                f[v]=max(f[v],f[v-c[i]]+w[i]);
 //               printf("v=%d\tf[%d]=%d\n",v,v,f[v]);
            }
        }



    }
    return 0;
}



/*
1
5 10
2 3 4 5 1
4 3 2 1 5*/
View Code

相关文章:

猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-03
  • 2022-12-23
  • 2021-06-11
相关资源
相似解决方案