HDU 1114 Piggy-Bank
完全背包
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=500+10; const int INF=0x7FFFFFFF; int P[maxn],W[maxn]; int dp[10000+10]; int main() { int T; scanf("%d",&T); while(T--) { int a,b,n,i,j; scanf("%d%d",&a,&b); b=b-a; scanf("%d",&n); for(i=1; i<=n; i++) scanf("%d%d",&P[i],&W[i]); for(i=0; i<=b; i++) dp[i]=INF; dp[0]=0; for(i=1; i<=n; i++) for(j=W[i]; j<=b; j++) if(dp[j-W[i]]!=INF) dp[j]=min(dp[j],dp[j-W[i]]+P[i]); if(dp[b]==INF) printf("This is impossible.\n"); else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[b]); } return 0; }