关闭

hdu1114完全背包

标签: c语言dfs
170人阅读 评论(0) 收藏 举报
分类:

Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19728    Accepted Submission(s): 10016


Problem Description
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.
 
Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19728    Accepted Submission(s): 10016



Problem Description

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.


 

#include<iostream>
#include<memory.h>
using namespace std;
int min(int x, int y)
{
        if(x<y)
        return x;
        else
                return y;
}
int main()
{
        int a[101], f[1000];
        int e, u, n, x, k = 0;
        int t, i, j, m;
        int p[101], w[101];
        cin >> t;
        while (t--)
        {
                cin >> e >> u >> n;
                x = u - e;
                for (m = 0; m <= x; m++)
                {
                        f[m] = 10000;
                }
                f[0] = 0;
                //cout<<f[x]<<endl;
                for (i = 0; i < n; i++)
                {
                        cin >> p[i] >> w[i];
                }
                for (i = 0; i < n; i++)
                {
                        for (j = w[i]; j <= x; j++)
                        {

                                f[j] = min(f[j], f[j - w[i]] + p[i]);
                        }
                }

                a[k] = f[x];
                k++;
        }
        for (i = 0; i < k; i++)
        {
                if (a[i] != 10000)
                        {
                        cout << "The minimum amount of money in the piggy-bank is " << a[i] << endl;
                }
                else {
                        cout << "This is impossible.";
                }

        }

}

0
0
查看评论

HDU1114(完全背包)

传送门:点击打开链接题目大意:给你一个背包的最小值和最大值  然后给几种钱,问背包装满最少的钱为多少,如果装不满就输出impossible。解题思路:完全背包,注意一下边界AC代码如下:#include&amp;lt;iostream&amp;gt; #include&am...
  • calculate23
  • calculate23
  • 前天 22:21
  • 1

HDU1114(完全背包)

题意: 往储蓄罐里放钱,给出储蓄罐的重量,和总重量,以及钱的重量和单价。询问最少用多少钱可以装满盒子 思路: 初始化最大值,裸完全背包 #include #include #include #include #include using namespace std; int voc...
  • qq_33951440
  • qq_33951440
  • 2017-05-08 21:35
  • 168

hdu1114(完全背包)

完全背包 基础题:http://acm.hdu.edu.cn/showproblem.php?pid=1114 #include #include #include #include #include using namespace std; #define N 505 #define ...
  • u013899738
  • u013899738
  • 2015-08-23 10:05
  • 197

hdu1114 完全背包

完全背包的基础上变化了一点点 把这几题补完了,昨晚在宿舍打题时,有点懵 #include #include #include #include using namespace std; const int maxn = 507, maxW = 1e4 + 7; //最多507种货币 i...
  • baisedeqingting
  • baisedeqingting
  • 2017-07-07 10:25
  • 97

HDU1114完全背包

#include #include #include using namespace std; #define INF 9999999 int p[10005];//硬币的价值 int w[10005];//硬币的重量 int dp[10005];//体积为i时可以存放多少钱 int min...
  • ZCR_7
  • ZCR_7
  • 2014-07-06 20:36
  • 365

HDU2602/HDU1114/HDU2191(重新整理一下01背包,完全背包,多重背包)

好长时间不做背包的问题,有一点遗忘,现在把这些问题整理一下~ 一.01背包(HDU2602) 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题意就是普通的01背包,给出n种物品和背包容量,给出每种物品的重量和价值,求当前背包最多能达到的价值.(c...
  • riba2534
  • riba2534
  • 2017-01-11 13:24
  • 1110

hdu1114(完全背包+恰好装满)

给定n个物品,每个物品可以随便取,求恰好装满时的最少价值,f[0]=0,f数组初始化为INT_MAX表示不存在讲背包装满的解 状态转移方程:f[j]=min( f [ j ] , f[ j ] - w [ i ] + v [ i ] ); 代码如下: #include #include #inc...
  • u013509299
  • u013509299
  • 2014-02-04 22:10
  • 513

hdu1114 Piggy-Bank(完全背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1114 题意:存钱罐可以往里面放一些价值小的钱,但是时间久了就不知道里面有多少钱了,除非你打破它。现在给出空罐子的重量和最满能装到多重,然后给出每种硬币的价值和重量,我们要在不打破它的情况下确认罐子里最少有多...
  • Flynn_curry
  • Flynn_curry
  • 2016-02-12 23:18
  • 1493

HDU1114 &&POJ1384:Piggy-Bank(完全背包)

Problem Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this acti...
  • libin56842
  • libin56842
  • 2013-06-07 15:40
  • 5651

Piggy-Bank—hdu1114完全背包

Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19651 ...
  • Litter_Limbo
  • Litter_Limbo
  • 2016-05-27 21:29
  • 188
    个人资料
    • 访问:1590次
    • 积分:133
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档