https://vjudge.net/problem/HDU-1114
找了个完全背包的题,用了滚动数组,空间复杂度为n(背包大小),时间复杂度为n*m(物品数量)
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
import java.util.*;
public class Main {
static class item{
int val;
int weight;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
n = input.nextInt();
while((n--)!=0){
int a,b;
a = input.nextInt();
b = input.nextInt();
a = b - a;
int m;
m = input.nextInt();
item u[] = new item[m];
for(int i=0;i<m;i++){
u[i] = new item();
}
for(int i=0;i<m;i++){
u[i].val = input.nextInt();
u[i].weight = input.nextInt();
}
int dp[] = new int[a+1];
Arrays.fill(dp,-1);
dp[0] = 0;
for(int i=0;i<m;i++){
for(int j=u[i].weight;j<=a;j++){
if(dp[j-u[i].weight] != -1){
if(dp[j] == -1) dp[j] = dp[j-u[i].weight]+u[i].val;
else dp[j] = Math.min(dp[j-u[i].weight]+u[i].val,dp[j]);
}
}
}
if(dp[a] == -1) System.out.println("This is impossible.");
else System.out.println("The minimum amount of money in the piggy-bank is " + dp[a]+".");
}
}
}