HDU 2191 Full Backpack

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.