动态规划:完全背包问题-HDU1114-Piggy-Bank
Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24389 Accepted Submission(s): 12367
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!
the weight of an empty pig and of the pig filled with coins. Both weights 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 an integer number N (1 <= N <= 500) that gives
the number of various coins used in the given currency. Following this are exactly N lines, each specifying one 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, W is it's
weight in grams.
can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
The minimum amount of money in the piggy-bank is 100.
This is impossible.
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std; const int INF = 0x7ffffff;//可以这样规定最大值
struct t
{
int we;
int va;
} ty[510];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int e,f;
scanf("%d%d",&e,&f);
int W = f - e;
int d[10100];
int n;
scanf("%d",&n);
for(int i=1;i<=W;i++)
d[i] = INF;
d[0] = 0;//很关键的初始化
for(int i=0; i<n; i++)
{
scanf("%d%d",&ty[i].va,&ty[i].we);
for(int j=ty[i].we;j<=W;j++)
{
if(d[j] > d[j-ty[i].we] + ty[i].va)
d[j] = d[j-ty[i].we] + ty[i].va;
}
}
if(d[W] == INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",d[W]);
}
return 0;
}
动态规划:完全背包问题-HDU1114-Piggy-Bank的更多相关文章
- [Dynamic Programming]动态规划之背包问题
动态规划之背包问题 例题 现有4样物品n = ['a', 'b', 'c', 'd'],重量分别为w = [2, 4, 5, 3],价值分别为v = [5, 4, 6, 2].背包最大承重c = 9. ...
- 记录结果再利用的"动态规划"之背包问题
参考<挑战程序设计竞赛>p51 https://www.cnblogs.com/Ymir-TaoMee/p/9419377.html 01背包问题 问题描述:有n个重量和价值分别为wi.v ...
- 动态规划_01背包问题_Java实现
原文地址:http://blog.csdn.net/ljmingcom304/article/details/50328141 本文出自:[梁敬明的博客] 1.动态规划 什么是动态规划?动态规划就是将 ...
- 【动态规划/多重背包问题】POJ1014-Dividing
多重背包问题的优化版来做,详见之前的动态规划读书笔记. dp[i][j]表示前i中数加得到j时第i种数最多剩余几个(不能加和得到i的情况下为-1)递推式为: dp[i][j]=mi(dp[i-1][j ...
- 【Luogu】【关卡2-15】动态规划的背包问题(2017年10月)【还差一道题】
任务说明:这是最基础的动态规划.不过如果是第一次接触会有些难以理解.加油闯过这个坎. 01背包二维数组优化成滚动数组的时候有坑有坑有坑!!!必须要downto,downto,downto 情景和代码见 ...
- python 动态规划(背包问题和最长公共子串)
背包问题 现在要往一个可以装4个单位重量的背包里怎么装价值最高:A重量1个单位,价值15:B重量3个单位,价值20:C重量4个重量,价值30 使用动态规划填充空格 class SolutionBag: ...
- poj 1742 Coins (动态规划,背包问题)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 32977 Accepted: 11208 Descripti ...
- 九度OJ 1455 珍惜现在,感恩生活 -- 动态规划(背包问题)
题目地址:http://ac.jobdu.com/problem.php?pid=1455 题目描述: 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元, ...
- 记忆搜索与动态规划——DP背包问题
题目描述 01背包问题 有n个重量和价值分别为\(w_i,v_i\)的物品.从这些物品中挑选出总重量不超过W的物品,求所有挑选方案中价值中总和的最大值. 限制条件 1 <= n <= 10 ...
- 用js实现动态规划解决背包问题
动态规划的原理: 移至到该同学的博文中,讲解的声动易懂 https://www.jianshu.com/p/a66d5ce49df5 现在主要是用js来实现动态规划 function bb(v, w, ...
随机推荐
- 批量 多个JPG生产PDF .net C#
using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.Collections.Generic; us ...
- X64下IIS调用32位的dll
WebAPI项目中遇到了需要调用32位C++的dll的情况,调试的时候能正常调用,但是发布了之后部署在IIS中出现了BadFormatImage异常, 解决方法是在IIS中相应应用程序池=>高级 ...
- Int与String之间相互转换
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
- iOS编程规范(整理)
一.文档结构管理 1.建立Libraries文件夹,所有第三方库放入其中. 2.建立Utilities文件夹,自已封装的类放入其中. 3.建立Constants.h头文件,所有的常量定义于其中.Con ...
- java 千分位的添加和去除
转至:http://blog.sina.com.cn/s/blog_8f99a1640102v1xh.html 将一个数字转换为有千分位的格式: NumberFormat numberFormat1 ...
- cms-详细页面-3
1.设置上一条.下一条数据 2.使用昌言插件 3.点击链接帖子的访问数加一 1.在mapper中设置分页: <?xml version="1.0" encoding=&quo ...
- C++ vector容器类型的用法及注意
转自http://www.cnblogs.com/charley_yang/archive/2010/12/11/1903040.html vector类为内置数组提供了一种替代表示,与string类 ...
- linux 命令——46 vmstat(转)
vmstat 是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行 ...
- 如何解析比特币中的交易原始数据rawData
交易数据结构 有关交易的详细信息可以查看比特币的wiki网站:Transaction TxBinaryMap: 原始图片地址 交易的结构表格(Transaction): 示例数据 以一个正式网络的一笔 ...
- 在ListView控件中实现修改功能
实现效果: 知识运用: ListView控件的LabelEdit属性 //指示用户是否可以编辑控件中数据项的标签 public bool LabelEdit{get;set;} 实现代码: priva ...