The first thought of the method is very complex, with a try to hold the mentality submitted, really tle
Then search, is a complete backpack, a lot of do not remember, re-review a bit.
The solution of the first annotation is tle, the second and the third are feasible, but the ideas are different.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;const int inf= 0x7fffffff; #define N 10005int dp[n],p[505],w[505];void init () {//memset (dp,-1,sizeof (DP)); for (int i=0;i<n;i++) dp[ i]=inf;dp[0]=0;} int main () {#ifndef online_judgefreopen ("In.txt", "R", stdin), #endifint t,n,e,f;scanf ("%d", &t), while (t--) {init () ; scanf ("%d%d%d", &e,&f,&n), for (int i=1;i<=n;i++) scanf ("%d%d", &p[i],&w[i]);/*for (int i=1;i <=f-e;i++) {//This method is considered directly from the scale of N, and obviously does not use the advantages of DP for (int j=1;j<=n;j++) {for (int k=0;k*w[j]<=i;k++) {if (Dp[i-k*w[j] ]!=inf) dp[i]=min (dp[i],dp[i-k*w[j]]+p[j]*k);}} *//*for (int i=1;i<=n;i++) {////ex-i-1 to pre-I-type for (int j=w[i];j<=f-e;j++) {if (Dp[j-w[i]]!=inf) dp[j]=min (dp[j],dp[ J-w[i]]+p[i]);}} */for (int i=1;i<=f-e;i++) {//weight is I, n kinds of items can reach the minimum value, here do not consider the number of J items, because I is small to large, then each time we calculate the value is automatically followed by the number of k-1 values for (int j=1; j<=n;j++) {if (I-w[j]>=0&&dp[i-w[j]]!=inf) dp[i]=min (Dp[i],dp[i-w[j]]+p[j]);}} if (dp[f-e]==inf) printf ("This is impossible.\ n "), Else printf (" The minimum amount of money in the Piggy-bank is%d.\n ", dp[f-e]);} return 0;}
HDU 1114 Piggy-bank