hdoj1114Piggy-Bank完全背包问题 smart piggy bank

Piggy-Bank

TimeLimit: 2000/1000 MS(Java/Others)MemoryLimit: 65536/32768 K (Java/Others)
Total Submission(s):7675AcceptedSubmission(s): 3837


Problem DescriptionBefore ACM can do anything, a budget must be prepared and thenecessary financial support obtained. The main income for thisaction comes from Irreversibly Bound Money (IBM). The idea behindis simple. Whenever some ACM member has any small money, he takesall the coins and throws them into a piggy-bank. You know that thisprocess is irreversible, the coins cannot be removed withoutbreaking the pig. After a sufficiently long time, there should beenough cash in the piggy-bank to pay everything that needs to bepaid.

But there is a big problem with piggy-banks. It is not possible todetermine how much money is inside. So we might break the pig intopieces only to find out that there is not enough money. Clearly, wewant to avoid this unpleasant situation. The only possibility is toweigh the piggy-bank and try to guess how many coins are inside.Assume that we are able to determine the weight of the pig exactlyand that we know the weights of all coins of a given currency. Thenthere is some minimum amount of money in the piggy-bank that we canguarantee. Your task is to find out this worst case and determinethe minimum amount of cash inside the piggy-bank. We need yourhelp. No more prematurely brokenpigs!

InputThe input consists of T test cases. The number of them (T) is givenon the first line of the input file. Each test case begins with aline containing two integers E and F. They indicate the weight ofan empty pig and of the pig filled with coins. Both weights aregiven in grams. No pig will weigh more than 10 kg, that means 1<= E <= F <= 10000. Onthe second line of each test case, there is an integer number N (1<= N <= 500) that gives the number ofvarious coins used in the given currency. Following this areexactly N lines, each specifying one coin type. These lines containtwo integers each, Pand W (1 <= P <=50000, 1 <= W <=10000). P is thevalue of the coin in monetary units, W is it's weight ingrams.
hdoj1114Piggy-Bank完全背包问题 smart piggy bank

OutputPrint exactly one line of output for each test case. The line mustcontain the sentence "The minimum amount of money in the piggy-bankis X." where X is the minimum amount of money that can be achievedusing coins with the given total weight. If the weight cannot bereached exactly, print a line "This isimpossible.".

Sample Input
3 10110 2 1 1 30 50 10 110 2 1 1 50 30 1 6 2 10 3 20 4
Sample Output
Theminimum amount of money in the piggy-bank is 60. The minimum amountof money in the piggy-bank is 100. This is impossible.
SourceCentral Europe 1999
RecommendEddy

解决代码:#include<iostream>using namespace std;
int main(){ int t; cin>>t; while(t--) { int e,f,n,p[50001],w[10001],temp[10001]; cin>>e>>f; cin>>n; for(int i=1;i<=n;++i) { cin>>p[i]>>w[i]; } for(int i=1;i<=f-e;++i) { temp[i]=INT_MAX; } temp[0]=0; for(int i=1;i<=n;++i) { for(intj=w[i];j<=f-e;++j) { if(temp[j-w[i]]!=INT_MAX) { if(temp[j-w[i]]+p[i]<temp[j]) temp[j]=temp[j-w[i]]+p[i]; } } } if(temp[f-e]<INT_MAX) cout<<"The minimum amount of money inthe piggy-bank is"<<temp[f-e]<<"."<<endl; else cout<<"This isimpossible."<<endl; } return 0;}
分析:
这个问题非常类似于01背包问题,所不同的是每种物品有无限件。也就是从每种物品的角度考虑,与它相关的策略已并非取或不取两种,而是有取0件、取1件、取2件……等很多种。如果仍然按照解01背包时的思路,令f[i][v]表示前i种物品恰放入一个容量为v的背包的最大权值。

状态转移方程为:v[i][j]=min{v[i-1][j],v[i][j-w[i]]+p[i]}。

最后,如果动规数组最后一格数据不等于初始值,即为答案。


  

爱华网本文地址 » http://www.413yy.cn/a/25101016/290500.html

更多阅读

ipad5.1.1完美越狱教程 ipad5.1.1完美越狱

ipad5.1.1完美越狱教程——简介iOS5.1.1完美越狱工具Absinthe 2.0.2已经发布,最新的Absinthe 2.0.2完美越狱工具可以支持iPhoen4S 5.1.1完美越狱、iPad2 5.1.1完美越狱,当然,iPhone4 5.1.1完美越狱也是完全没问题的。ipad5.1.1完美越

怎样治疗便秘 便秘吃什么最快排便

排便的次数不同的人是不一样的,很多人每日一次,有些人隔日一次或每周3次。这些都属于正常范围。便秘是指排便给人带来不舒适的感觉,一般表现为排便次数减少和大便秘结。便秘给很多人的生活带来麻烦,长期便秘有引发痔疮和直肠癌的可能。

背包问题算法 背包问题递归

0/1背包问题 1. 问题描述 给定一个载重量为m,n个物品,其重量为wi,价值为vi,1<=i<=n,要求:把物品装入背包,并使包内物品价值最大 2. 问题分析 在0/1背包问题中,物体或者被装入背包,或者不被装入背包,只有两种选择。 循环变量i,j意义:前i个物品能

谢国忠的忠告:房价过高是一个危险信号二

总理管不住总经理的涨价冲动2009年10月22日,中国国家统计局公布的数据显示,该年度前三季度中国经济同比增长7.7%,其中第三季度增速达到了8.9%,中国经济“保8”完全没有问题。但是又带来另外一个挑战,资产价格的上升对于通胀究竟会带来多大

训诂学的声训方法 训诂学自考资料

训诂学的声训方法声训是与形训、义训相对而言,是训诂学中的一种重要的训诂方法。语音是语言的物质外壳,而字是记录语言的一个工具,因而仅靠字形来确定字义是无法完全解决问题的,这不仅因为古代用字出现通假现象,更因为形声字的产生,形声

声明:《hdoj1114Piggy-Bank完全背包问题 smart piggy bank》为网友緈諨尐爺分享!如侵犯到您的合法权益请联系我们删除