首页 > 代码库 > 杭电2602 Bone Collector

杭电2602 Bone Collector

Bone Collector

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


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
 
题目大意:有一个骨头收集者,他有一个容量为V的包,他要收集N根骨头,每根骨头拥有它的价值Valu和占用体积Vol,求最优解。
 
这是个简单的01背包问题,核心算法就是:F[i,v] = max{F[i ? 1,v],F[i ? 1,v ? C i ] + W i } 具体的解释请看我的另一片博文:http://www.cnblogs.com/William-xh/p/7305877.html
 
主要就是还是要进行判断,如果背包放得下的话,那就是要看我放还是不放;如果是放不下的话,那就是不放。
 
这题我是用二维数组解的,一位数组那个代码还没有研究过,就也还没写过。之后要是写的话就码上来。
附上代码:
#include <iostream>
#include<math.h>
#include <iomanip>
#include<cstdio>
#include<string>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<sstream>
#include<string.h>
#include<stdio.h>
using namespace std;
int dp[1001][1001];

int max(int x,int y)
{
    return x>y?x:y;
}

int main()
{
   int t;
   cin>>t;
   int n,v;
   int vol[1001],value[1001];
   while(t--)
   {
       cin>>n>>v;//记录骨头数量和总体积
       for(int iii=1;iii<=n;iii++)
       {
           cin>>value[iii];//记录值
       }
       for(int ii=1;ii<=n;ii++)
       {
           cin>>vol[ii];//记录体积
       }
       memset(dp,0,sizeof(dp));//把背包初始化为所有的情况价值都为0

        for(int i=1;i<=n;i++)//有几件物品
        {
            for(int j=0;j<=v;j++)//表示有几件物品时,你的背包容量在变化的同时,你背包的总价值是否发生了变化
            {
                if(vol[i]<=j)//当你的背包放得下第i件物品时
                {//就要考虑两种情况 1.放这件物品,也就是为这件物品腾出空间,然后计算剩下的空间放I-1件物品的最大值,这时候背包的价值=dp[i-1][j-vol[i]]+valu[i]
                 //2.不放这件物品,就是只考虑在这样的容量下,放i-1件物品的情况。这时候 背包的价值为: dp[i-1][j]
                 //所以我们就要比较他们的最大值
                 dp[i][j]=max(dp[i-1][j],dp[i-1][j-vol[i]]+value[i]);
                }
                else{//当放不下这件物品的时候(要是只有一个公式的话 ij 可能会负)
                    dp[i][j]=dp[i-1][j];
                }

            }
        }
        cout<<dp[n][v]<<endl;
   }
    return 0;
}

 

 

杭电2602 Bone Collector