POJ 1384【完全背包】

题意:
已知储蓄罐满时的质量f以及空时质量e,
有n种硬币,每种硬币的价值为p,质量为w,
求该储蓄罐中的最少有多少钱?
思路:
完全背包思想,问题是在一个重量下的最小价值
那么只要变一下符号就好了?

//#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
using namespace std;

typedef __int64 LL;

const int INF=0x3f3f3f3f;
const int N=1e4+10;
int w[550],v[550];
int dp[N];
int W;
int x,y;
int n;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&x,&y);
        W=y-x;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d%d",&v[i],&w[i]);
        memset(dp,INF,sizeof(dp));
        dp[0]=0;
        for(int i=0;i<n;i++)
            for(int j=w[i];j<=W;j++)
                dp[j]=min(dp[j-w[i]]+v[i],dp[j]);
        if(dp[W]==INF)
            printf("This is impossible.
");
        else
            printf("The minimum amount of money in the piggy-bank is %d.
",dp[W]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934855.html