其他分享
首页 > 其他分享> > Bone Collector (HDU - 2602 )

Bone Collector (HDU - 2602 )

作者:互联网

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

InputcopyOutputcopy
1
5 10
1 2 3 4 5
5 4 3 2 1
14

这个题目就是给你背包容量来找最大价值,就是01背包,放或者不放的问题,首先还是将背包和容量对应保存,先是价格再是容量。记得每次需要memset(结构体),要是用数组存的话可能需要注意一下。

AC代码:

#include<iostream>
#include<algorithm>
using  namespace std;
const int N=1010;

struct node{//capacity 容量,value 价值 ans 当前容量下的最大价值
    int capacity,value,ans;
}st[N];

int main(){
    int t,n,m;
    cin>>t;
    while(t--){
        cin>>n>>m;
        memset(st,0,sizeof(st));
        for(int i=1;i<=n;i++) cin>>st[i].value;
        for(int i=1;i<=n;i++) cin>>st[i].capacity;
        for(int i=1;i<=n;i++)
            for(int j=m;j>=st[i].capacity;j--)//当会st[j-st[i].capacity].ans+st[i].value时就是当前容量中能放入i这个物品时价格的时候背包的价值能够大于不放这个物品时候的价值
                st[j].ans=max(st[j].ans,st[j-st[i].capacity].ans+st[i].value);
        cout<<st[m].ans<<endl;
    }
    return 0;
}

 

标签:HDU,capacity,2602,int,value,st,ans,Collector,bone
来源: https://www.cnblogs.com/jerrytangcaicai/p/16271355.html