HDU1114(完全背包装满问题)
Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19849 Accepted Submission(s): 10086
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!
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
int n,W;
int v[MAXN],w[MAXN];
int dp[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int e,f;
scanf("%d%d",&e,&f);
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&v[i],&w[i]);
}
W=f-e;
for(int i=;i<MAXN;i++)
{
dp[i]=INF;//大前提:背包装满.因为求获得的价值最小,所以需要将dp初始化正无穷.若求价值最大,则将dp初始化为负无穷
}
dp[]=;//背包需要装满,则将dp[0]初始化为0
for(int i=;i<n;i++)
{
for(int j=w[i];j<=W;j++) //完全背包:逆序
dp[j]=min(dp[j-w[i]]+v[i],dp[j]);//求最小的价值
}
if(dp[W]==INF)
{
printf("This is impossible.\n");
}
else
{
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[W]);
}
} return ;
}
HDU1114(完全背包装满问题)的更多相关文章
- HDU-1114 完全背包+恰好装满问题
B - Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...
- hdu1114 完全背包
题意:给出钱罐的重量,然后是每种钱的价值和重量,问钱罐里最少可能有多少钱. 完全背包. 代码: #include<iostream> #include<cstdio> #inc ...
- 01背包模板、全然背包 and 多重背包(模板)
转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...
- hdu3496 二维01背包
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M ...
- Partial Tree---hdu5534(完全背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意:有n个节点,让这n个节点形成一棵树,这棵树的种类有很多种,现在告诉n-1个f[i],表示度 ...
- POJ3260——The Fewest Coins(多重背包+完全背包)
The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...
- Piggy-Bank(完全背包)
/* http://acm.hdu.edu.cn/showproblem.php?pid=1114 完全背包问题 再判断背包能否装满 题意: 给出存空钱罐的重量以及装满时的重量 给出每种硬币的面值以及 ...
- HDU 2546 饭卡(0-1背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 题意: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金 ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
随机推荐
- Python菜鸟之路:Python基础-Socket基础-1
预热知识 OSI 七层模型 谈到TCP/IP,就不得不说OSI七层模型,OSI 是国际标准化组织(ISO)和国际电报电话咨询委员会(CCITT)联合制定的开放系统互连参考模型,为开放式互连信息系统提供 ...
- 【windows】远程桌面报错:由于CredSSP加密Oracle修正
对于windows家庭版用户,无法打开gepdit.msc需要手动修改注册表 创建红色部分目录 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curre ...
- python基础12 ---函数模块2
函数模块 一.sys函数模块详解 1.sys.argv[x] 功能:从程序外部接受参数,接收的参数个数可以是多个,在程序内部sys.argv吧这些外部参数转换成元组的形式,然后以索引x的方式在内部取出 ...
- PHP eval函数使用介绍
eval()函数中的eval是evaluate的简称,这个函数的作用就是把一段字符串当作PHP语句来执行. 复制代码代码如下: eval("echo'hello world';") ...
- 《程序员代码面试指南》第一章 栈和队列 最大值减去最小值小于或等于num的数量
题目 给定整数数组arr和整数num,共返回多少的数组满足如下情况 max(arr[i...j]) - min(arr[i...j]) <= num max(arr[i...j])表示数组arr ...
- linux操作系统使用中的一些总结
VIM常用命令 gg //到第一行 (N)G //到第n行(N为整数) G //到最后一行 0 //到行头 $ //到行尾 (N)dd //删除N行,并将内容保存到粘贴板 p ...
- OJ的runtime error exit code对应SIGTERM代码
Signal Name Number Description SIGHUP 1 Hangup (POSIX) SIGINT 2 Terminal interrupt (ANSI) SIGQUIT 3 ...
- Map集合按照value和key进行排序
最近由于特殊的业务需求,需要做相关数据排序,下面就贴出其中的将map集合中按照value或者key进行排序的代码,后面再具体详说. /** * map 集合排序 * @param map * @ret ...
- POJ-3126 暑假集训-搜索进阶F题
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/F 经验就是要认真细心,要深刻理解.num #include& ...
- Html 练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...