HDOJ 2192多重背包 01背包模板

来源:互联网 发布:java笔试基础题 编辑:程序博客网 时间:2024/04/30 16:30


Input

输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。

Output

对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。

Sample Input

18 22 100 44 100 2

Sample Output

400

Author

lcy

Source

#include<iostream>#include<cstdio>#include<string.h>using namespace std;int dp[105],v,n[105],p[105],w[105];void ZeroOnePack(int cost,int value){     for(int i=v; i>=cost; i--)///逆序          dp[i]=max(dp[i],dp[i-cost]+value);}void CompletePack(int cost,int value){     for(int i=cost; i<=v; i++)///正序          dp[i]=max(dp[i],dp[i-cost]+value);}void MultiPack(int cost,int value,int num){     int k;     if( num*cost>=v)         CompletePack(cost,value);     else{          k=1;          while( num>k){                 ZeroOnePack(k*cost,k*value);                 num-=k;                 k*=2;          }          ZeroOnePack(num*cost,num*value);     }}int main(){    int c,m,i;    scanf("%d",&c);    while( c--){           scanf("%d%d",&v,&m);           for( i=1; i<=m; i++)                scanf("%d%d%d",&p[i],&w[i],&n[i]);///单价,每袋重量,袋数           memset(dp,0,sizeof(dp));           for( i=1; i<=m; i++)                MultiPack(p[i],w[i],n[i]);           printf("%d\n",dp[v]);    }}

hdu 2602 01背包 bone collecter

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

15 101 2 3 4 55 4 3 2 1

Sample Output

14

Author

#include<iostream>#include<cstdio>#include<string.h>using namespace std;int main(){    int T,n,v,dp[1001],value[1001],volume[1001];    scanf("%d",&T);    while(T--)    {        memset(dp,0,sizeof(dp));        scanf("%d%d",&n,&v);        for(int i=1; i<=n; i++)            scanf("%d",&value[i]);        for(int i=1; i<=n; i++)            scanf("%d",&volume[i]);        for(int i=1; i<=n; i++)            for(int j=v; j>=volume[i]; j--)                dp[j]=max(dp[j-1],dp[j-volume[i]]+value[i]);        printf("%d\n",dp[v]);    }    return 0;}



0 0
原创粉丝点击