题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114

题面:

Problem Description
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea 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!

 
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate 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.
 
Output
Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".
 
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.
 
思路:由于每个硬币的数量是不限的,所以本题是完全背包。将初始化处理一下除了dp[i][0](一维时dp[0])设为0外其他的都设为无穷大,然后就是一个裸的完全背包。
 
代码实现如下:
 #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(多重背包+一维和二维通过方式)的更多相关文章

  1. 51Nod1085 0-1背包(一维和二维数组实现)

    背包是典型的动态规划问题,关于背包问题的详解,推荐博客:点击打开链接(这篇博客有点错误,代码for循环里错了,不过讲解 的很详细) 题目如下: 在N件物品取出若干件放在容量为W的背包里,每件物品的体积 ...

  2. Mojo 返回一维和二维数组

    这种情况不断的网数组@arr2里放入数据,返回的内容为: 这种情况是一维数组: while( $selStmt->fetch() ){ print "\$a1 is $a1\n&quo ...

  3. 一维和二维ST模板

    void init(){ ; i < n; i++) st[i][] = a[i]; ; ( << j) <= n; j++){ ; i + ( << j) - & ...

  4. C#如何定义一个变长的一维和二维数组

    1.假设将要定义数组的长度为程序执行过程中计算出来的MAX List<int> Arc = new List<int>(); ; i < MAX; i++) { Arc. ...

  5. np.unique()对一维和二维数组去重

    numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)[source] 一 ...

  6. Vijos1392拼拼图的小衫[背包DP|二维信息DP]

    背景 小杉的幻想来到了经典日剧<死亡拼图>的场景里……被歹徒威胁,他正在寻找拼图(-.-干嘛幻想这么郁闷的场景……). 突然广播又响了起来,歹徒竟然又有了新的指示. 小杉身为新一代的汤浅, ...

  7. OpenCV: Kmeans的使用一维和二维点集

    OpenCVKmeans算法默认使用了Kmeans++选取种子点 参考:OpenCv中Kmeans算法实现和使用 //效果:根据半径聚类,并不一定能得到好的结果. float CBlotGlint:: ...

  8. hdu2159二维费用背包

    题目连接 背包九讲----二维费用背包 问题 二维费用的背包问题是指:对于每件物品,具有两种不同的费用:选择这件物品必须同时付出这两种代价:对于每种代价都有一个可付出的最大值(背包容量).问怎样选择物 ...

  9. hdu2159FATE(二维背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=2159 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在 ...

随机推荐

  1. 实验吧密码学:RSAROLL

    原题: {920139713,19} 704796792 752211152 274704164 18414022 368270835 483295235 263072905 459788476 48 ...

  2. phpcms V9如何判断用户是否登录以及登陆后的标签写法问题

    首先要获取userid {php$userid=param::get_cookie('_userid');}​ 然后再判断是否为空 {if $userid}...这里写已经登录之后的代码...{els ...

  3. Chromium之工程类别

    虽然有700多个project,其实有很多是不成声二进制执行文件的,他们主要是调用cygwin的环境,执行一些python的脚本. 具体这个.py文件做了哪些共工作,还要再研究,目前看到有打包一些.p ...

  4. MySQL、HBase、ES的特点和区别

    MySQL:关系型数据库,主要面向OLTP,支持事务,支持二级索引,支持sql,支持主从.Group Replication架构模型(本文全部以Innodb为例,不涉及别的存储引擎). HBase:基 ...

  5. bzoj3546[ONTAK2010]Life of the Party

    题意是裸的二分图关键点(必然在二分图最大匹配中出现的点).比较经典的做法在cyb15年的论文里有: 前几天写jzoj5007的时候脑补了一种基于最小割可行边的做法:考虑用最大流求解二分图匹配.如果某个 ...

  6. BZOJ4810 Ynoi2017由乃的玉米田(莫队+bitset)

    多组询问不强制在线,那么考虑莫队.bitset维护当前区间出现了哪些数,数组记录每个数的出现次数以维护bitset.对于乘法,显然应有一个根号范围内的因子,暴力枚举即可.对于减法,a[i]-a[j]= ...

  7. CentOS 挂载(U盘NTFS格式,新硬盘,增加交换分区,扩展根分区等)

    1.挂载fat或者fat32分区的U盘 如果是用VM安装的linux,在vm里挂载U盘有两个前提: 第一,主机里的service要启动: 第二,U盘是连接到虚拟机,而不是主机,需要确认这点: 2.使用 ...

  8. [洛谷P5107]能量采集

    题目大意:有一张$n(n\leqslant50)$个点$m(m\leqslant n(n-1))$条边的有向图,每个点还有一个自环,每个点有一个权值.每一秒钟,每个点的权值会等分成出边个数,流向出边. ...

  9. BZOJ3123:[SDOI2013]森林——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3123 https://www.luogu.org/problemnew/show/P3302 树上主 ...

  10. BZOJ1412 [ZJOI2009]狼和羊的故事 【最小割】

    1412: [ZJOI2009]狼和羊的故事 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3454  Solved: 1733 [Submit][ ...