杭电 1114 Piggy-Bank【完全背包】
解题思路,首先很容易想到方程f[v]=min(f[v],f[v-w[i]+p[i]),因为是要求当包装满的时候(因为题目中给出的是包的质量是一定的),包里面装的钱最少,所以将f[]初始化成一个很大的数。
然后对于这个循环
for(i=1;i<=n;i++)
{
for(v=w[i];v<=m;v++)
f[v]=min(f[v],f[v-w[i]+p[i]);//可以理解为只要不超过包的容量,你可以任意放入该种硬币。
}
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<stdio.h>
#define max 1000000000
int p[50005],w[10010],f[10010];
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
int ncase,i,v,m,e,r,n;
while(scanf("%d",&ncase)!=EOF)
{
while(ncase--)
{
for(i=1;i<10010;i++)
f[i]=max;
scanf("%d %d",&e,&r);
m=r-e;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d %d",&p[i],&w[i]); for(i=1;i<=n;i++)
{
for(v=w[i];v<=m;v++)
f[v]=min(f[v],f[v-w[i]]+p[i]);
}
if(f[m]<max)
printf("The minimum amount of money in the piggy-bank is %d.\n",f[m]);
else
printf("This is impossible.\n"); } }
}
杭电 1114 Piggy-Bank【完全背包】的更多相关文章
- 饭卡------HDOJ杭电2546(还是01背包!!!!!!)
Problem Description 电子科大本部食堂的饭卡有一种非常诡异的设计,即在购买之前推断剩余金额. 假设购买一个商品之前,卡上的剩余金额大于或等于5元,就一定能够购买成功(即使购买后卡上剩 ...
- Big Event in HDU(杭电1171)(多重背包)和(母函数)两种解法
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 杭电 2159 fate(二维背包费用问题)
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 杭电 1114 Piggy-Bank 完全背包问题
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 杭电dp题集,附链接还有解题报告!!!!!
Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱 最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f ...
- 杭电ACM题单
杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...
- 杭电acm习题分类
专注于C语言编程 C Programming Practice Problems (Programming Challenges) 杭电ACM题目分类 基础题:1000.1001.1004.1005. ...
- acm入门 杭电1001题 有关溢出的考虑
最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...
随机推荐
- DB2常用运维命令
DB2是IBM公司推出关系型数据库管理系统.主要应用于银行.医院等大型机构.现今DB2主要包含以下三个系列:DB2 for Linux, UNIX and Windows(LUW) . DB2在Lin ...
- zabbix监控超详细搭建过程(转)
监控及zabbix 目录: 1 监控分类... 1 1.1 硬件监控... 1 1.2 系统监控... 2 1.3 网络监控... 3 1.4 ...
- 洛谷P1466 集合 Subset Sums_01背包水题
不多解释,适当刷刷水… Code: #include<cstdio> #include<algorithm> using namespace std; const int ma ...
- 前端开发—BOM对象DOM文档对象操作
BOM 浏览器对象 BOM:Browser Object Model 操作浏览器,需要调用window对象,它是所有浏览器都支持的对象,表示的就是浏览器窗口 window对象可以通过点调用子对象 wi ...
- 安装ubuntu14.04之后要做的一些事
前言: 用ubuntu14.04也有一段时间了,感觉与之前版本相比还是在挺多方面有了改进.但刚装完还是有一些小问题需要自己动手解决.鉴于网上的内容太过零碎,有些方案也太过老旧,因此在这里为大家总结一些 ...
- Python IO编程-读写文件
1.1给出规格化得地址字符串,这些字符串是经过转义的能直接在代码里使用的字符串 需要导入os模块 import os >>>os.path.join('user','bin','sp ...
- maven项目发布后访问jsp页面报错
type Exception report message java.lang.NullPointerException description The server encountered an i ...
- 【codeforces 723F】st-Spanning Tree
[题目链接]:http://codeforces.com/contest/723/problem/F [题意] 给你一张图; 让你选择n-1条边; 使得这张图成为一颗树(生成树); 同时s的度数不超过 ...
- JavaScript 事件对内存和性能的影响
程序代码: <%-- Created by IntelliJ IDEA. User: 乔克叔叔 Date: 2017/12/26 Time: 16:45 To change this templ ...
- SGU 185 Two shortest
Two shortest Time Limit: 500ms Memory Limit: 4096KB This problem will be judged on SGU. Original ID: ...