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背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...
随机推荐
- 转载:Jmeter教程索引
摘自: 阳光温暖了心情 的 http://www.cnblogs.com/yangxia-test/category/431240.html 1 JMeter学习(一)工具简单介绍 2 JMeter学 ...
- Hadoop集群datanode死掉或者secondarynamenode进程消失处理办法
当Hadoop集群的某单个节点出现问题时,一般不必重启整个系统,只须重启这个节点,它会自动连入整个集群. 在坏死的节点上输入如下命令即可: hadoop-daemon.sh start datanod ...
- tomcat java变量环境设置
绿色版tomcat 手动启动startup.bat的时候出现一闪而过的状态.解决方法,配置startup.bat文件 @echo off SET JAVA_HOME=C:\Program Files ...
- JDK1.6.0+Tomcat6.0的安装配置
JDK1.6.0+Tomcat6.0的安装配置是如何进行的呢?我们按照下面几个步骤来: 1.安装JDK 这是进行JSP开发的重要一步,也是安装JSP引擎(Tomcat.Resin.Weblogic等) ...
- 【WPF】XAML引入资源和在C#代码中动态添加样式
转载自: http://blog.csdn.net/honantic/article/details/48781543 XAML引入资源参考这里: http://blog.csdn.net/qq_18 ...
- C语言 · 龟兔赛跑预测
基础练习 龟兔赛跑预测 时间限制:1.0s 内存限制:512.0MB 锦囊1 模拟. 问题描述 话说这个世界上有各种各样的兔子和乌龟,但是研究发现,所有的兔子和乌龟都有一个共 ...
- 如何打一手好Log
如果项目上过线的话,那你一定知道Log是多么重要. 为什么说Log重要呢?因为上线项目不允许你调试,你只能通过Log来分析问题.这时打一手好Log的重要性绝不亚于写一手好代码.项目出问题时,你要能拿出 ...
- R语言绘制沈阳地铁线路图
##使用leaflet绘制地铁线路图,要求 ##(1)图中绘制地铁线路 library(dplyr) library(leaflet) library(data.table) stations< ...
- mysql慢查询日志开启和存储格式
mysql版本号是mysql5.6.22.安装环境windows7. 1.使用该查询日志能够找到有效率问题的sql语句.并记录下来,进行监控. 能够使用例如以下语句查询和设置慢查询日志 (1) 查看慢 ...
- Mysql 常用工具
mysqladmin:用于管理MySQL服务器的客户端 mysqladmin是一个执行管理操作的客户程序.可以用它来检查服务器的配置和当 前的状态,创建并删除数据库等等. 这样调用mysqladmin ...