杭电2602--Bone Collector(01背包…

来源:互联网 发布:数据采集卡原理设计 编辑:程序博客网 时间:2024/06/06 16:06

Problem Description

Many years ago , in Teddy’s hometown there was aman who was called “Bone Collector”. This man like to collectvaries 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 histrip of collecting there are a lot of bones , obviously , differentbone has different value and different volume, now given the eachbone’s value along his trip , can you calculate out the maximum ofthe total value the bone collector can get ?

 

 

 

 

Input

The first line contain a integer T , the number ofcases.
Followed by T cases , each case three lines , the first linecontain two integer N , V, (N <= 1000 , V<= 1000 )representing the number of bones and thevolume of his bag. And the second line contain N integersrepresenting the value of each bone. The third line contain Nintegers representing the volume of each bone.

Output

One integer per line representing the maximum ofthe 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

 

# include<stdio.h>

# include<string.h>

int f[2000],V;

void ZeroOnePack(int cost,int weight)

{

     int v;

     for(v=V;v>=cost;v--)

          if(f[v]<f[v-cost]+weight)

                f[v]=f[v-cost]+weight;

}

int main()

{

     int i,t,n,c[1001],w[1001];

     scanf("%d",&t);

     while(t--)

     {

          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]);

          memset(f,0,sizeof(f));

          for(i=0;i<n;i++)

                ZeroOnePack(c[i],w[i]);

          printf("%d\n",f[V]);

     }

     return 0;

}

 

 

0 0