HDU 1114 Piggy-bank Piggy storage tank (AC code) full backpack (full capacity, minimum value)

Source: Internet
Author: User

1#include <iostream>2 #defineMAX 0XFFFFFFF3 using namespacestd;4 //requirements: 1, just fill 2, the total value of the smallest5 intvalue[501];6 intweight[501];7 intdp[10010];8 intMinintAintb)9 {Ten     returnA<b?a:b; One } A intCalintVintN//space, type - { -     inti,j; thedp[0]=0; -      for(i=1; i<=v;i++) -dp[i]=MAX; -      for(i=1; i<=n;i++) +          for(j=weight[i];j<=v;j++) -         { +Dp[j]=min (Dp[j], Dp[j-weight[i]] +value[i]); A         } at     returnDp[v]; - } - voidMain () - { -     intT; -scanf"%d",&T); in     intE,f,n,ans; -      while(t--) to     { +         inti; -scanf"%d%d", &e,&f);//empty cans, full cans thescanf"%d", &n);//How many kinds of coins *          for(i=1; i<=n;i++)     $         {Panax Notoginsengscanf"%d%d", &value[i],&weight[i]);//value, Weight -         } theAns=cal (fe,n); +         if(ans==MAX) Aprintf"This is impossible.\n"); the         Else +printf"The minimum amount of money in the Piggy-bank is%d.\n", ans); -     } $}
1114

Test instructions: To save a coin in a piggy bank, know the weight of the empty tank and the weight to be filled, can it be loaded? If you can, print the minimum value. (Note: Can be installed coin weight must be just good, the total value of the inside to achieve minimum)

input: Includes t Test example , given in the first line. Next comes t example, The first line of each example consists of two integers e and f representing the weight of the empty tank and the weight of the full amount of money, respectively, The units are grams.  1 <= E <= F <= 10000.  The second row contains an integer n, which represents the type of coin. (1 <= n <= 500) Next n n The information of the coins, each line has two integers p and w

Output : Each output line, if can be loaded, print the minimum amount of money in the Piggy-bank is X. where x is the sum of the sum. If it cannot be loaded, print this is impossible.

Idea: Like a complete backpack, the difference is that the weight of the coin should be exactly, not more or less, the total value is minimal, which is contrary to the complete knapsack problem. Need to deal specifically with these two problems, the weight is right, then when updating the DP to ensure this, the total price is minimal, then in comparison with the min instead of Max.

HDU 1114 Piggy-bank Piggy storage tank (AC code) full backpack (full capacity, minimum value)

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.