In order to save the lives of compatriots in the disaster area, you are prepared to purchase some food support disaster areas, now suppose you have a total of funds N, and the market has m rice, each rice is bagged products, its price range, and can only buy the whole bag.
Excuse me: How many kilograms of grain can you purchase with limited funds?
Sample INPUT1 8 2 2 100 4 4 100 2
Sample Output400
1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <algorithm>5 using namespacestd;6 Const intmaxn= the;7 8 intDP[MAXN];9 intVALUE[MAXN];//the price per bagTen intWEIGHT[MAXN];//the weight of each bag One intBAG[MAXN];//Number of bags A intNvalue,nkind; - - //0-1 backpack, at cost, to get the value of weight the voidZeroonepack (intCostintweight) - { - for(inti=nvalue;i>=cost;i--) -Dp[i]=max (dp[i],dp[i-cost]+weight); + } - + //full backpack, cost, value for weight A voidCompletepack (intCostintweight) at { - for(inti=cost;i<=nvalue;i++) -Dp[i]=max (dp[i],dp[i-cost]+weight); - } - - //Multiple Backpack in voidMultiplepack (intCostintWeightintamount) - { to if(cost*amount>=nvalue) Completepack (cost,weight); + Else - { the intk=1; * while(k<amount) $ {Panax NotoginsengZeroonepack (k*cost,k*weight); -amount-=K; thek<<=1; + } AZeroonepack (Amount*cost,amount*weight);//don't forget it, it's often dropped. the } + } - intMain () $ { $ intT; -scanf"%d",&T); - while(t--) the { - //This DP initialization must not be forgotten, it can be initialized to 0 without filling,Wuyi //otherwise dp[0]=0, the rest is-inf. theMemset (DP,0,sizeof(DP)); -scanf"%d%d",&nvalue,&nkind); Wu for(intI=0; i<nkind;i++) -scanf"%d%d%d",&value[i],&weight[i],&bag[i]); About for(intI=0; i<nkind;i++) $ Multiplepack (Value[i],weight[i],bag[i]); -printf"%d\n", Dp[nvalue]); - } - return 0; A}
HDU 2191 Full Backpack