HDU1114Piggy-Bank(完全背包)
Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19735 Accepted Submission(s): 10020
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!
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.
weight. If the weight cannot be reached exactly, print a line "This is impossible.".
3
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 60.
The minimum amount of money in the piggy-bank is 100. This is impossible./*题目大意:已知猪灌所能容纳的重量,然后告诉若干硬币的价值与重量。求使得用已知硬币装入猪灌
* 中使得猪灌中硬币价值总和最小 ,且要求猪灌必须被装满,若不能装满则输出 This is impossible.
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std; const int maxn = 999999;
#define mem(a) memset(a, 0, sizeof(a))
int dp[10010]; //dp[i]表示所装重量为i时候的最小价值
struct node {
int p, w;
}a[550]; int main() {
int t;
scanf("%d",&t);
while (t --) {
mem(a);
mem(dp);
int e, f;
scanf("%d%d",&e, &f);
e = f-e;
for (int i = 0; i<=e; i++) dp[i] = maxn;
dp[0] = 0;
int n;
scanf("%d",&n);
for (int i = 1; i<=n; i++) scanf("%d%d",&a[i].p, &a[i].w);
for (int i = 1; i<=n; i++) {
for (int j = a[i].w; j<=e; j++) {
dp[j] = min(dp[j], dp[j-a[i].w] + a[i].p);
}
}
if (dp[e] == maxn) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[e]);
}
return 0;
}
HDU1114Piggy-Bank(完全背包)的更多相关文章
- BZOJ 1531: [POI2005]Bank notes( 背包 )
多重背包... ---------------------------------------------------------------------------- #include<bit ...
- bzoj1531: [POI2005]Bank notes(多重背包)
1531: [POI2005]Bank notes Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 521 Solved: 285[Submit][Sta ...
- 【多重背包小小的优化(。・∀・)ノ゙】BZOJ1531-[POI2005]Bank notes
[题目大意] Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出 ...
- 【bzoj1531】[POI2005]Bank notes 多重背包dp
题目描述 Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出面值 ...
- bzoj 1531 Bank notes 多重背包/单调队列
多重背包二进制优化终于写了一次,注意j的边界条件啊,疯狂RE(还是自己太菜了啊啊)最辣的辣鸡 #include<bits/stdc++.h> using namespace std; in ...
- 2018.09.08 bzoj1531: [POI2005]Bank notes(二进制拆分优化背包)
传送门 显然不能直接写多重背包. 这题可以用二进制拆分/单调队列优化(感觉二进制好写). 所谓二进制优化,就是把1~c[i]拆分成20,21,...2t,c[i]−2t+1+1" role= ...
- bzoj1531: [POI2005]Bank notes
Description Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我 ...
- DSY1531*Bank notes
Description Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我 ...
- Hdu 2955 Robberies 0/1背包
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
随机推荐
- iOS 实现后台 播放音乐声音 AVAudioPlayer 以及铃声设置(循环播放震动)
1.步骤一:在Info.plist中,添加"Required background modes"键,value为:App plays audio 或者: 步骤二: - (BOOL) ...
- samba 搭建
#useradd -M -s /sbin/nologin kvmshare #mkdir /home/etl #chown kvmshare:kvmshare /home/etl 将本地账号添加到 s ...
- 【Hdu2089】不要62(数位DP)
Description 题目大意:给定区间[n,m],求在n到m中没有"62"或"4"的数的个数. 如62315包含62,88914包含4,这两个数都是不合法的 ...
- hash_equals()函数
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/92 了解下hash_equals的概念: bool hash_e ...
- 视觉SLAM的数学表达
相机是在某些时刻采集数据的,所以只关心这些时刻的位置和地图. 就把这一段时间的运动变成了李三时刻 t=1,2,...K当中发生的事情. 在这些事可,x表示机器自身的位置. x1,x2,x3,x4... ...
- Golang 中的坑 一
Golang 中的坑 短变量声明 Short variable declarations 考虑如下代码: package main import ( "errors" " ...
- 安装MySQL时提示3306端口已被占用的解决方案
之前安装过mysql,用的好好的,但是今天开启服务时报异常,无法启动.为省事,于是想到卸载重装,安装过程中发现3306已经被占用,这也是一开始服务无法启动的原因. 看到有人说用fport查看端口号,于 ...
- SQL2005 到 SQL2008R2 发布订阅----发布'xxxxx'的初始快照尚不可用。
步骤略! SQL2005 到 SQL2008R2 发布订阅----发布'xxxxx'的初始快照尚不可用. 发布库快照已经创建完成为什么到订阅就快照不可用呢! 订阅通过日志读取代理解析! 查了下代理安全 ...
- 阿里云EMR集群初始化后的开发准备工作
前言:EMR的集群使用越来越普遍,但是每一次的集群释放到集群的重新创建,期间总有一些反复的工作需要查询与配置.为方便后续工作查阅,现在对集群初始化后的工作进行大概的梳理如下. ...
- python3基础(一)
1. python文件主程序入口文件一般来要申明python路径,编码信息,作者说明等: #!/usr/bin/env python # _*_ coding: utf-8 _*_ # Author: ...