Well.. Recently practicing the basic DP
This problem is quite simple (haha), but I just want to say that there is a detail here.
First Test instructions:
There is a T-set sample, and then give the start weight of the piggy bank e, end the weight f (that is, when it is full of change), and then give you a number n, which means that there are now n kinds of coins.
Then the next n lines, each line has two numbers p,w,p represents the value of this type of change, W represents the weight of this type of change, the amount of change is unlimited.
Then you have to output the case where the current weight is f (i.e. the weight is just f) and the minimum amount of change is required.
Ideas:
Obviously, this is obviously a complete backpack. But the only thing to be aware of here is the initialization, because we're going to completely fill the piggy bank, so we initialize the dp[0]=0, and then because we're asking for the least amount of change, the rest is initialized to infinity.
In addition to the above situation, I believe that the rest is not difficult.
When Dp[v]==inf, it means that there is no solution at this time.
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < set> #include <map> #include <math.h>using namespace std; #define MAXN 10010#define inf 99999999int DP[MAXN ];int V[555],w[555];int Main () {int t;scanf ("%d", &t), while (t--) {int e,f;scanf ("%d%d", &e,&f); int v=f-e; int n;scanf ("%d", &n), for (int i=1;i<=n;i++) {scanf ("%d%d", &v[i],&w[i]);} Fill (dp,dp+maxn+1,inf);DP [0]=0;for (int i=1;i<=n;i++) {for (int j=w[i];j<=v;j++) {dp[j]=min (dp[j],dp[j-w[i]]+v [i]);}} if (dp[v]==inf) printf ("This was impossible.\n"), Else printf ("The minimum amount of money in the Piggy-bank is%d.\n", Dp[v]) ;}} /*310 11021 130 50*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU (1114)--piggy-bank (full backpack)