题目链接: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. lintcode-156-合并区间

    156-合并区间 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [ ...

  2. iOS- UITableViewCell对象是怎么重用的 ?

    iOS设备的内存有限,如果用UITableView显示成千上万条数据, 就需要成千上万个UITableViewCell对象的话, 那将会耗尽iOS设备的内存.要解决该问题,需要重用UITableVie ...

  3. windows批处理学习---01

    一. 标记符号: CR(0D) 命令行结束符 Escape(1B) ANSI转义字符引导符 Space() 常用的参数界定符 Tab() ; = 不常用的参数界定符 + COPY命令文件连接符 * ? ...

  4. 第17天:CSS引入、选择器优先级(中级)

    一.CSS 位置 1.行内式  css <div class="fr" style="color:red;">aa</div> 2. 内 ...

  5. 【Python】python基础_代码编写注意事项

    1. 说明使用的编译方式 1 #!/usr/bin/python 2. 说明字符编码方式 1 #coding=utf-8 3. print 默认输出是换行的,如果要实现不换行需要在变量末尾加上逗号 # ...

  6. 配置bond和vlan

    网卡是光口还是电口的方法ethtool 网卡名字 一看速度二看port是否是firber首先查看需要做bond的物理网卡,如enp130s0f0,enp131s0f0以物理网卡为enp130s0f0, ...

  7. BZOJ2654 & 洛谷2619:tree——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2654 https://www.luogu.org/problemnew/show/P2619 给你 ...

  8. BZOJ3526 [Poi2014]Card 【线段树】

    题目链接 BZOJ3526 题解 思来想去,发现很显然可以用线段树维护 每个区间保存所有合法方案的左右端点[当左端点一定是,右端点当然存最小的那个就行了] 这么整的数,\(\frac{1}{1000} ...

  9. mysql 读写分离实现资料

    以下很多链接需要 FQ才能看到,稍后会整理翻译成中文! Easy Read/Write Splitting with PHP’s MySQLnd https://blog.engineyard.com ...

  10. bzoj2120: 数颜色(BIT套主席树+set/分块)

    带修改的 HH的项链. 带修改考虑用BIT套主席树,查区间里有几个不同的数用a[i]上次出现的位置pre[i]<l的数有几个来算就好了. 考虑怎么修改.修改i的时候,我们需要改变i同颜色的后继的 ...