HDU1114_Piggy-Bank(背包/完全背包)

解题报告

题目传送门

题意:

给金币的面额和重量,求装满储蓄罐的最小价值。

思路:

完全背包基础,初始dp为最大,dp[0]=0表示储蓄罐为空价值为0;

状态转移方程就是dp[j]=min(dp[j],dp[j-w[i]]+c[i])

#include <iostream>
#include <cstring>
#include <cstdio>
#define inf 99999999
using namespace std;

int main()
{
    int t,i,j,e,f,n,v,w[550],p[550],dp[10010];
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&e,&f);
        v=f-e;
        scanf("%d",&n);
        for(i=1; i<=n; i++) {
            scanf("%d%d",&p[i],&w[i]);
        }
        for(i=1; i<=v; i++)
            dp[i]=inf;
        for(i=1; i<=n; i++) {
            for(j=w[i]; j<=v; j++) {
                dp[j]=min(dp[j],dp[j-w[i]]+p[i]);
            }
        }
        if(dp[v]<inf)
            printf("The minimum amount of money in the piggy-bank is %d.\n",dp[v]);
        else printf("This is impossible.\n");
    }
    return 0;
}

Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11691    Accepted Submission(s): 5892

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.

HDU1114_Piggy-Bank(背包/完全背包),布布扣,bubuko.com

时间: 2024-10-12 19:50:13

HDU1114_Piggy-Bank(背包/完全背包)的相关文章

POJ2063_Investment(背包/完全背包)

解题报告 题意: 本金买股票,最大n年后的收益(本金加利息) 思路: 基础完全背包,单纯的写可能TLE,因为股票都是1000的倍数,所以本金用1000的整数倍来买股票. #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 using namespace std; int w[12],c[12],dp[2001000],v; int n,d; int main() {

HDU1864_最大报销额(背包/01背包)

解题报告 题目传送门 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define inf 99999999 using namespace std; int v,w[35],d[4],dw1,sum,dp[31*1000*100]; int main() { double Q,dw; int n,i,j,m,t; char c; while(~sc

HDU2602_Bone Collector(背包/01背包)

解题报告 题目传送门 题意: 容量为v的大小,物品数n,每个物品有价值和容量,求能装进包的最大价值. 思路: 基础01背包. dp[j]=max(dp[j],dp[j-c[i]]+w[i]) #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 using namespace std; int main() { int t,i,j,n,v,w[1010],c[1010]

POJ 3260 多重背包+完全背包

前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168.1.1就能看到他的路由器的一切信息,包括密码,然后打开笔记本……好了,废话不多说,能连上网后第一时间当然是继续和队友之前约好的训练了. 今天翻看到之前落下的一道混合背包题目,然后在草稿本上慢慢地写递推方程,把一些细节细心地写好…(本来不用太费时间的,可是在家嘛,一会儿妈走来要我教她玩手机,一会儿有一个亲戚朋

HDU1248_寒冰王座(背包/完全背包)

解题报告 题目传送门 无聊的水题. #include <iostream> #include <cstring> #include <cstdio> #define inf 0x3f3f3f3f using namespace std; int c[10100],w[10100],dp[11000],v,n; int main() { int t,i,j; scanf("%d",&t); while(t--) { memset(dp,0,si

HDU2191_悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(背包/多重背包)

解题报告 题目传送门 题意: 中文不多说; 思路: 基础多重背包,每个物品有多个可以选,转换成01背包解. #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 using namespace std; int main() { int t,i,j,n,m,v,p,h,cc,w[1010],c[1010],dp[1010]; scanf("%d",&

B 二维背包+完全背包 Hdu2159

<span style="color:#3333ff;">/* ------------------------------------------------------------------------------------------------ author : Grant Yuan time : 2014.7.19 aldorithm: 二维背包+完全背包 ----------------------------------------------------

B 维背包+完全背包 Hdu2159

<span style="color:#3333ff;">/* ------------------------------------------------------------------------------------------------ author : Grant Yuan time : 2014.7.19 aldorithm: 二维背包+全然背包 ----------------------------------------------------

HDU 3591 The trouble of Xiaoqian(多重背包+01背包)

HDU 3591 The trouble of Xiaoqian(多重背包+01背包) http://acm.hdu.edu.cn/showproblem.php?pid=3591 题意: 有一个具有n种货币的货币系统, 每种货币的面值为val[i]. 现在小杰手上拿着num[1],num[2],-num[n]个第1种,第2种-第n种货币去买价值为T(T<=20000)的商品, 他给售货员总价值>=T的货币,然后售货员(可能,如果小杰给的钱>T, 那肯定找钱)找钱给他. 售货员每次总是用