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