HDU 1114 Piggy-Bank(完全背包问题)【完全背包--刚好装满 模板】
来源:互联网 发布:直播电视软件 编辑:程序博客网 时间:2024/04/20 08:33
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!
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!
310 11021 130 5010 11021 150 301 6210 320 4
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<cstdio>#include<algorithm>#include<cstring>using namespace std;const int N=505;const int inf=1e9+7;int value[N],w[N];int dp[100005];int m,n;int minw,maxw;int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&minw,&maxw); int ww=maxw-minw; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d%d",&value[i],&w[i]); for(int i=0;i<=ww;i++)//注意是ww 不是n 因为这WA了好多遍 dp[i]=inf; dp[0]=0; for(int i=0;i<n;++i) { for(int j=w[i];j<=ww;++j) { dp[j]=min(dp[j],dp[j-w[i]]+value[i]); } } if(dp[ww]>=inf) printf("This is impossible.\n"); else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[ww]); } return 0;}
阅读全文
1 0
- HDU 1114 Piggy-Bank(完全背包问题)【完全背包--刚好装满 模板】
- hdu 1114 Piggy-Bank(必须装满的完全背包)
- hdu 1114 Piggy Bank (完全背包 要求正好装满)
- HDU 1114 Piggy-Bank 完全背包模板
- HDU-1114 Piggy-Bank(完全背包问题)
- hdu 1114 Piggy-Bank 完全背包问题
- Piggy-Bank (hdu 1114 完全背包 模板)
- hdu 1114 Piggy-Bank(完全背包)
- hdu 1114 Piggy-Bank(完全背包)
- hdu 1114 Piggy-Bank(完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- hdu 1114 Piggy-Bank(完全背包)
- hdu 1114 Piggy-Bank (完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- HDU 1114 Piggy-Bank(完全背包)
- HDU - 1114 - Piggy-Bank (完全背包)
- 【自动化测试】Jenkins 环境搭建
- 函数表达式和函数声明
- 闲来无事写一写拿网站做App数据数据的
- 思维导图--数据结构导论(3)
- JavaScript原型和闭包之---构造函数、原型与对象三者之间的关系(扩充constructor)(五)
- HDU 1114 Piggy-Bank(完全背包问题)【完全背包--刚好装满 模板】
- 辗转相除法求最大公约数
- URL 和URI的区别
- radio checked 没有反应
- HDU 1081 To The Max
- 斐波那契数列生成
- react flux的坑之(三) npm install 慢,或执行 npm instal之后run以后报错少包
- |1191|C语言实验——整数位
- UISlider 滑动结束触发 valueChange 动作方法