HDU1114

题意
给一个储钱罐,已知空的储钱罐和装了硬币的储钱罐的质量,然后给了n种硬币的质量和价值,问储钱罐里最少有多少钱
分析
完全背包
注意要初始化为 INF,要正好装满,如果结果是INF,输出This is impossible.
初始化dp[0] = 0,其余都为INF,因为只有开始时0转移过来的才是合法的
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5+10;
int dp[maxn];
int st,ed;
int n;
int c[maxn], a[maxn];
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &st, &ed);
        int v = ed - st;
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &c[i], &a[i]);
        }
        for(int i = 1;i <= 1e5; i++)
            dp[i]=1e9;
       // dp[v]=1e9;
        dp[0]=0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = a[i]; j <= v; j++)
            {
                dp[j] = min(dp[j], dp[j-a[i]] + c[i]);
            }
        }
        if(dp[v]!=1e9)
        printf("The minimum amount of money in the piggy-bank is %d.
", dp[v]);
        else
            printf("This is impossible.
");
    }
    return 0;
}
View Code

 

要么优秀要么生锈
原文地址:https://www.cnblogs.com/Superwalker/p/7878363.html