POJ 1384 Piggy-Bank(完全背包)
Description
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!
Input
Output
Sample Input
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
Sample Output
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.
思路:
1. 完全背包, 且要求恰好装满, dp[0] = 0
2. dp[i][j] 表示前 i 件物品放入容量为 j 的背包的最小价值, dp[i][j] = min(dp[i-1][j-k*w[i]]+k*v[i]])
3. 求 min, 要求初始化为 INF
代码:
#include <iostream>
using namespace std; const int INF = 0X3F3F3F3F;
int E, F, N;
int v[510], w[510];
int dp[10010]; int solve_dp() {
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
int V = F-E;
for(int i = 0; i < N; i++) {
for(int j = w[i]; j <= V; j++) {
dp[j] = min(dp[j], dp[j-w[i]]+v[i]);
}
}
return dp[V];
} int main() {
freopen("E:\\Copy\\ACM\\测试用例\\in.txt", "r", stdin);
int tc;
cin >> tc;
while(tc--) {
scanf("%d%d%d", &E, &F, &N);
for(int i = 0; i < N; i++)
scanf("%d%d", &v[i], &w[i]);
// mainfunc
int ans = solve_dp();
if(ans == INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n", ans);
}
return 0;
}
POJ 1384 Piggy-Bank(完全背包)的更多相关文章
- poj 1384 Piggy-Bank(全然背包)
http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- POJ 1384 Piggy-Bank (完全背包)
Piggy-Bank 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/F Description Before ACM can d ...
- poj 1384 Piggy-Bank(完全背包)
Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10830 Accepted: 5275 Descr ...
- POJ 1384 Piggy-Bank【完全背包】+【恰好完全装满】(可达性DP)
题目链接:https://vjudge.net/contest/217847#problem/A 题目大意: 现在有n种硬币,每种硬币有特定的重量cost[i] 克和它对应的价值val[i]. 每 ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 3181 Dollar Dayz(全然背包+简单高精度加法)
POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...
- POJ 3211 Washing Clothes(01背包)
POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...
- POJ 1384 POJ 1384 Piggy-Bank(全然背包)
链接:http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- POJ 1384 Piggy-Bank (ZOJ 2014 Piggy-Bank) 完全背包
POJ :http://poj.org/problem?id=1384 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode ...
- POJ 1384 Piggy-Bank 背包DP
所谓的全然背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...
随机推荐
- HttpClient 教程 (四)
第四章 HTTP认证 HttpClient提供对由HTTP标准规范定义的认证模式的完全支持.HttpClient的认证框架可以扩展支持非标准的认证模式,比如NTLM和SPNEGO. 4.1 用户凭证 ...
- C++访问mysql数据库
C++连接mysql数据库,并取数据进行显示本例中,在mysql中已经存在了一个数据库test,并在test数据库中创建了一张表stu做测试,表中包含3个字段 需要把mysql目录下的libmysql ...
- IDEA用maven打war包
打包其实很简单: 把tomcat停掉,点击Maven Projects ,点击clean , 点击package 自动打包完成. 查看包位置: 这是包的位置,通过查看PATH就可以知道你的包在哪里了. ...
- lighttpd + php 移植配置
buildroot 内添加 lighttpd 和php 等相关选项 // make menuconfig Target packages ---> Interpreter languages a ...
- ArrayDeque
ArrayDeque是一个基于数组的,非线程安全的,没有容量大小限制的双端队列实现 下面这张图就是添加了一些元素的数据结构图,其中head指向数据结构中的头部元素,tail指向数据结构中最后一个元素. ...
- openwrt内核配置选项添加
摘自:http://blog.csdn.net/weiniliuchao/article/details/50295527 增加内核配置选项 openwrt的.config文件中,关于内核的选项都是形 ...
- linux下获取服务器硬件信息的脚本
这是个简单的脚本,便于查询服务器的硬件信息: #!/bin/bash # # Description: # Used to get the hardware config information. # ...
- jQuery EasyUI教程之datagrid应用-2
二.DataGrid的扩展应用 上一份教程我们创建的一个CRUD应用是使用对话框组件来增加或编辑用户信息.本教程将教你如何创建一个CRUD 数据表格(datagrid). 为了让这些CRUD功能正常工 ...
- 基于Dedup的数据打包技术
基于Dedup的数据打包技术 0.引言 Tar, winrar, winzip是最为常见的数据打包工具软件,它们把文件集体封装成一个单独的数据包,从而方便数据的分布.传输.归档以及持久保存等目的 ...
- 关于Cocos2d-x中音效重复播放问题的解决
在做一些动作的时候,有时候只希望播放一次音效,但是触发音效的前提条件是要按着某个按钮,如果直接把播放音效的语句写在MOVED的case中,就会重复播放音效 解决方法就是把播放音效的语句写在BEGAN的 ...