In fact, this problem in the winter vacation self-study training game did, but feel the understanding now and then really not the same, a freshman half a year later, changed so much, we are in progress, a back then came out so far ...
Okay, it's a simple question, a backpack, a sign on the line.
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<map>#include<algorithm>using namespacestd;Long Longdp[10010];intmoney[10010], weight[10010];intMain () {intT; scanf ("%d",&t); while(t--) { intw1,w2; scanf ("%d%d",&w1,&W2); intall = W2-W1; dp[0] =0; for(inti =1; I <= all;i++) Dp[i]=999999999; intkind; scanf ("%d",&kind); for(inti =0; I < kind;i++) {scanf ("%d%d",&money[i],&Weight[i]); } for(inti =0; I < kind;i++) { for(intj = Weight[i]; J <= all;j++) {Dp[j]= Min (Dp[j],dp[j-weight[i]] +Money[i]); } } if(Dp[all] = =999999999) printf ("This is impossible.\n"); Elseprintf"The minimum amount of money in the Piggy-bank is%lld.\n", Dp[all]); } return 0;}
HDU 1114 Piggy-bank