The main question: give the initial weight of the jar and the weight after filling it, then give the types of coins (currency and weight), and ask how much you can put at least after filling the jar.
Idea: Mainly pay attention to initialize, then write the state equation, Dp[v]=min (dp[v],dp[v-w[j]]+c[j]);
The code is as follows:
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#define MAX 1000000
using namespace Std;
int dp[10005];
int n,w1,w2,t,c[10005],w[10005];
int main ()
{
scanf ("%d", &t);
while (t--) {
Memset (Dp,0,sizeof (DP));
Memset (C,0,sizeof (c));
Memset (W,0,sizeof (w));
scanf ("%d%d", &W1,&W2);
scanf ("%d", &n);
for (int i=0;i<n;i++) {
scanf ("%d%d", &c[i],&w[i]);
}
for (int i=0;i<=w2-w1;i++) {
Dp[i]=max;
}
dp[0]=0;
for (int v=0;v<=w2-w1;v++) {
for (int j=0;j<=n;j++) {
if (V>=w[j])
Dp[v]=min (Dp[v],dp[v-w[j]]+c[j]);
}
}
if (Dp[w2-w1]==max) printf ("This is impossible.\n");
else printf ("The minimum amount of money on the Piggy-bank is%d.\n", DP[W2-W1]);
}
return 0;
}
POJ 1384 (full backpack)