HDU - 1114 Piggy-Bank(dp_完全背包)

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams.

Output

Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".

Sample Input

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

Sample Output

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

完全背包 && 多重背包(这位大哥讲的好 ~:https://www.cnblogs.com/aiguona/p/7274876.html

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 +7;
const int N = 1e5 + 10;

int w[N], v[N], dp[N];

int main()
{
    int E, F, V, t, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &E, &F);
        V = F - E;
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d%d", &v[i], &w[i]);
        }
        memset(dp, inf, sizeof(dp));
        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)
            cout<<"This is impossible."<<'\n';
        else
            cout<<"The minimum amount of money in the piggy-bank is "<<dp[V]<<'.'<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
8454次浏览 76人参与
# 你的实习产出是真实的还是包装的? #
1557次浏览 39人参与
# MiniMax求职进展汇总 #
23616次浏览 305人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7300次浏览 40人参与
# 简历第一个项目做什么 #
31444次浏览 320人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186719次浏览 1118人参与
# 巨人网络春招 #
11282次浏览 223人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152201次浏览 887人参与
# 研究所笔面经互助 #
118829次浏览 577人参与
# 重来一次,我还会选择这个专业吗 #
433235次浏览 3926人参与
# 简历中的项目经历要怎么写? #
309862次浏览 4177人参与
# 面试紧张时你会有什么表现? #
30460次浏览 188人参与
# 你今年的平均薪资是多少? #
212910次浏览 1039人参与
# AI时代,哪些岗位最容易被淘汰 #
63173次浏览 784人参与
# 我的求职精神状态 #
447925次浏览 3128人参与
# 你最满意的offer薪资是哪家公司? #
76352次浏览 374人参与
# 正在春招的你,也参与了去年秋招吗? #
363053次浏览 2635人参与
# 你怎么看待AI面试 #
179703次浏览 1220人参与
# 牛客AI文生图 #
21391次浏览 237人参与
# 职能管理面试记录 #
10773次浏览 59人参与
# 网易游戏笔试 #
6422次浏览 83人参与
# 腾讯音乐求职进展汇总 #
160527次浏览 1109人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务