首页 > 其他分享 >DP Problem Q:Piggy-Bank(HDU 1114)

DP Problem Q:Piggy-Bank(HDU 1114)

时间:2023-03-25 13:36:12浏览次数:34  
标签:HDU Bank money coins pig amount 1114 bank piggy


Problem Q

Time Limit : 2000/1000ms (Java/Other)   Memory Limit :65536/32768K (Java/Other)

Total Submission(s) : 2   Accepted Submission(s) : 1

Problem Description

Before ACM can do anything, abudget must be prepared and the necessary financial support obtained. The mainincome for this action comes from Irreversibly Bound Money (IBM). The ideabehind is simple. Whenever some ACM member has any small money, he takes allthe coins and throws them into a piggy-bank. You know that this process isirreversible, the coins cannot be removed without breaking the pig. After asufficiently long time, there should be enough cash in the piggy-bank to payeverything that needs to be paid. <br><br>But there is a big problemwith piggy-banks. It is not possible to determine how much money is inside. Sowe might break the pig into pieces only to find out that there is not enoughmoney. Clearly, we want to avoid this unpleasant situation. The onlypossibility is to weigh the piggy-bank and try to guess how many coins areinside. Assume that we are able to determine the weight of the pig exactly andthat we know the weights of all coins of a given currency. Then there is someminimum amount of money in the piggy-bank that we can guarantee. Your task isto find out this worst case and determine the minimum amount of cash inside thepiggy-bank. We need your help. No more prematurely broken pigs! <br>

 

 

Input

The input consists of T testcases. 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. Theyindicate the weight of an empty pig and of the pig filled with coins. Bothweights 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 aninteger number N (1 <= N <= 500) that gives the number of various coinsused in the given currency. Following this are exactly N lines, each specifyingone 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, Wis it's weight in grams. <br>

 

 

Output

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

 

 

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 inthe piggy-bank is 60.

The minimum amount of money inthe piggy-bank is 100.

This is impossible.

算法分析:

完全背包问题

代码实现:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int a[502],v[502],f[10002];
        int n,vp,vm,V;;
        cin>>vp>>vm;
        V=vm-vp;
        cin>>n;
        int i,j;
        for(i=0;i<n;i++)
        cin>>a[i]>>v[i];
        for(i=0;i<=V;i++)
            f[i]=99999999;//背包要求装满,恰好这题也是求最小值
            f[0]=0;//重要的初始化
        for(i=0;i<n;i++)
         for(j=v[i];j<=V;j++)
        {
            f[j]=min(f[j-v[i]]+a[i],f[j]);
        }

        if(f[V]!=99999999)
            cout<<"The minimum amount of money in the piggy-bank is "<<f[V]<<"."<<endl;
        else
            cout<<"This is impossible."<<endl;

    }
    return 0;
}


标签:HDU,Bank,money,coins,pig,amount,1114,bank,piggy
From: https://blog.51cto.com/u_14932227/6149355

相关文章

  • DP Problem R:Big Event in HDU(HDU 1171)
    ProblemRTimeLimit:10000/5000ms(Java/Other)   MemoryLimit:65536/32768K(Java/Other)TotalSubmission(s):1   AcceptedSubmission(s):0ProblemDesc......
  • DP Problem P:Bone Collector(HDU 2602
    ProblemPTimeLimit:2000/1000ms(Java/Other)   MemoryLimit:32768/32768K(Java/Other)TotalSubmission(s):1   AcceptedSubmission(s):1ProblemDescr......
  • 排序01背包 Problem W:Proud Merchants(HDU 3466)
    ProblemWTimeLimit:2000/1000ms(Java/Other)   MemoryLimit:131072/65536K(Java/Other)TotalSubmission(s):2   AcceptedSubmission(s):1ProblemDesc......
  • 【hdu6547】Tree(树链剖分, 线段树区间根号)
    problemalgorihtm1、树链剖分什么是树链剖分(重链剖分)?树链,就是树上的路径。剖分,就是把路径分类为重链和轻链。对于树上的一个点,与其相连的边中,连向的节点子树大小最大(重儿子)......
  • hdu 1853 Cyclic Tour(费用流OR二分图最佳匹配,5级)
    O- CyclicTourTimeLimit:1000MS     MemoryLimit:65535KB     64bitIOFormat:%I64d&%I64uSubmit StatusSystemCrawler (2013-05-30)......
  • 第6章 C++语言新特性111417专题一
    类型推导:auto/decltype语法:auto变量名称=值;(从值推导)​ decltype(表达式)变量名称[=值];(从表达式推导)功能:自动推导出变量名称的类型。案例:#include<iostream>us......
  • A strange lift HDU - 1548 (BFS)
    题意:第i个火车站都有一个数字Ki(0≤Ki≤N),火车在第i站只能前进Ki站或后退Ki站。火车只能在第1站和第N站之间行驶。请问,从第a站到第b站最少需前进或后退......
  • How Many Tables HDU - 1213(并查集/连通块数量)
    题意:朋友的朋友是朋友如果A认识B,B认识C,那么ABC三个人就可以坐在同一张桌子上但如果A认识B,C认识D,那我们就默认AB和CD不认识,需要准备两张桌子现在我们需要你计算出,我们一......
  • Red and Black HDU - 1312 (连通块的大小)
    题意:求某点所在连通块的大小。分析:由某点进行dfs,每次标记该点,并计数。#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=110,INF=......
  • 变形课 HDU - 1181 (dfs)
    题意:给定多个单词,每个单词的首字母可以到末字母,问能否由'b'到'm'。分析:将每个单词首尾字母建图,dfs('b')将能到的所有字母进行标记,最后检查'm'是否被标记。#include......