HDU 1114 Piggy-bank

Source: Internet
Author: User

Maximum value: The backpack must be filled accurately, when except dp[0] 0 other DP[1..V] are set to-∞

Minimum value: Requires just full pack, then at initialization except Dp[0] 0 other DP[1..V] are set to ∞
Precautions : The topic requires "Exactly full" , so when initializing. Other than dp[0] assignment to zero dp[i] are assigned a value of +∞. Because the topic is to find the minimum value. If the maximum value is calculated, then Dp[i] is assigned a value of-∞ , and if there is no qualification in the maximum value, it is assigned a value of 0. Notice the difference between the two types of questioning;


the code is as follows:
pay attention to the subscript problem
#include <stdio.h>int p[1008],w[1008];int dp[10000];int min (int a,int b) {return a>b?b:a;} int main () {   int e,f,v,t,i,j,n;   scanf ("%d", &t);   while (t--)   {      scanf ("%d%d", &e,&f);      V=F-E;      for (i=0;i<=v;i++)      dp[i]=9999999;      dp[0]=0;      scanf ("%d", &n);     for (i=0;i<n;i++)      scanf ("%d%d", &p[i],&w[i]);           for (i=0;i<n;i++) for        (j=w[i];j<=v;j++)         dp[j]=min (Dp[j],dp[j-w[i]]+p[i]);      if (dp[v]==9999999) printf ("This is impossible.\n");      else        printf ("The minimum amount of money on the Piggy-bank is%d.\n", Dp[v]);     }    return 0;}


HDU 1114 Piggy-bank

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.