Piggy-Bank(多重背包+一维和二维通过方式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114
题面:
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!
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 1e4 + ;
const int inf = 0x3f3f3f3f; int t, e, f, n, W;
/*int w[505], v[505], dp[maxn]; int main() {
scanf("%d", &t);
while(t--) {
scanf("%d%d", &e, &f);
W = f - e;
memset(dp, 0x3f3f3f3f, sizeof(dp));
scanf("%d", &n);
for(int i = 0; i < n; i ++) {
scanf("%d%d", &v[i], &w[i]);
}
dp[0] = 0;
for(int i = 0; i < n; i++) {
for(int j = w[i]; j <= W; j++) {
dp[j] = min(dp[j], dp[j - w[i]] + v[i]);
}
}
if(dp[W] >= inf) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[W]);
}
}
*/ int w[], v[], dp[][maxn]; int main() {
scanf("%d", &t);
while(t--) {
scanf("%d%d", &e, &f);
W = f - e;
memset(dp, 0x3f3f3f3f, sizeof(dp));
scanf("%d", &n);
for(int i = ; i < n; i++) {
scanf("%d%d", &v[i], &w[i]);
}
for(int i = ; i < n; i++) {
dp[i][] = ;
for(int j = ; j <= W; j++) {
if(j >= w[i]) {
dp[i + ][j] = min(dp[i][j], dp[i + ][j - w[i]] + v[i]);
} else {
dp[i + ][j] = dp[i][j];
}
}
}
if(dp[n][W] >= inf) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[n][W]);
}
}
Piggy-Bank(多重背包+一维和二维通过方式)的更多相关文章
- 51Nod1085 0-1背包(一维和二维数组实现)
背包是典型的动态规划问题,关于背包问题的详解,推荐博客:点击打开链接(这篇博客有点错误,代码for循环里错了,不过讲解 的很详细) 题目如下: 在N件物品取出若干件放在容量为W的背包里,每件物品的体积 ...
- Mojo 返回一维和二维数组
这种情况不断的网数组@arr2里放入数据,返回的内容为: 这种情况是一维数组: while( $selStmt->fetch() ){ print "\$a1 is $a1\n&quo ...
- 一维和二维ST模板
void init(){ ; i < n; i++) st[i][] = a[i]; ; ( << j) <= n; j++){ ; i + ( << j) - & ...
- C#如何定义一个变长的一维和二维数组
1.假设将要定义数组的长度为程序执行过程中计算出来的MAX List<int> Arc = new List<int>(); ; i < MAX; i++) { Arc. ...
- np.unique()对一维和二维数组去重
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)[source] 一 ...
- Vijos1392拼拼图的小衫[背包DP|二维信息DP]
背景 小杉的幻想来到了经典日剧<死亡拼图>的场景里……被歹徒威胁,他正在寻找拼图(-.-干嘛幻想这么郁闷的场景……). 突然广播又响了起来,歹徒竟然又有了新的指示. 小杉身为新一代的汤浅, ...
- OpenCV: Kmeans的使用一维和二维点集
OpenCVKmeans算法默认使用了Kmeans++选取种子点 参考:OpenCv中Kmeans算法实现和使用 //效果:根据半径聚类,并不一定能得到好的结果. float CBlotGlint:: ...
- hdu2159二维费用背包
题目连接 背包九讲----二维费用背包 问题 二维费用的背包问题是指:对于每件物品,具有两种不同的费用:选择这件物品必须同时付出这两种代价:对于每种代价都有一个可付出的最大值(背包容量).问怎样选择物 ...
- hdu2159FATE(二维背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2159 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在 ...
随机推荐
- 理解windows模型
同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回.按照这个定义,其实绝大多数函数都是同步调用(例如sin, isdigit等).但是一般而言,我们在说同步.异步的时候,特指 ...
- OSG学习:自动对齐节点示例
/********************************************************** *Write by FlySky *zzuxp@163.com http://w ...
- 解决连接mysql报错1130
最近在服务器上部署好的应用突然间连接不上mysql数据库,报错“ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this M ...
- C#中Console.ReadLine()和Console.Read()有何区别?
Console.Read 表示从控制台读取字符串,不换行. Console.ReadLine 表示从控制台读取字符串后进行换行. Console.Read() Console.ReadLine()方法 ...
- Dojo初探
Dojo 是一个由 Dojo 基金会开发的 Javascript 工具包, 据说受到 IBM 的永久支持,其包括四个部分: dojo, dijit, dojox, util dojo: 有时也被称作 ...
- C#中的反射和扩展方法的运用
前段时间做了一个练手的小项目,采用的是三层架构,也就是Models,IDAL,DAL,BLL 和 Web , 在DAL层中各个类中有一个方法比较常用,那就是 RowToClass ,顾名思义,也就是将 ...
- 如何设计好的RESTful API之安全性
保证RESTful API的安全性,主要包括三大方面: a) 对客户端做身份认证 b) 对敏感的数据做加密,并且防止篡改 c) 身份认证之后的授权 1.对客户端做身份认证,有几种常见的做法: 1)在请 ...
- codeforces 730 j.bottles
J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...
- Go语言【第二篇】:Go语法和数据类型
Go语言基础语法 Go标记 Go程序可以由多个标记组成,可以是关键字,标识符,常量,字符串,符号.如以下Go语句由6个标记组成: fmt.PrintIn("Hello, World!&quo ...
- hadoop的第一个hello world程序(wordcount)
在hadoop生态中,wordcount是hadoop世界的第一个hello world程序. wordcount程序是用于对文本中出现的词计数,从而得到词频,本例中的词以空格分隔. 关于mapper ...