题目链接: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. redis切换数据库的方法【jedis】

    package com.test; import redis.clients.jedis.Jedis; public class readredis { public static void main ...

  2. 语音信号处理之动态时间规整(DTW)(转)

    这学期有<语音信号处理>这门课,快考试了,所以也要了解了解相关的知识点.呵呵,平时没怎么听课,现在只能抱佛脚了.顺便也总结总结,好让自己的知识架构清晰点,也和大家分享下.下面总结的是第一个 ...

  3. c#中语句的先后顺序对结果的影响

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...

  4. Kafka性能之道

    Kafka高性能之道 高效使用磁盘 零拷贝 批处理和压缩 Partition ISR 高效使用磁盘 >顺序写cipan >Append Only(数据不更新,无记录级的数据删除,只会整个s ...

  5. ErrorUnable to tunnel through proxy. Proxy returns HTTP1.1 400 Bad Reques

    导入项目的时候,一般会出现这种错误,因为我们的gradle版本,不对,所以默认AS导入后,回去下载你需要的gradle,所以很慢, 先打开:项目路径底下的\gradle\wrapper\gradle- ...

  6. [剑指Offer] 54.字符流中的第一个不重复的字符

    题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...

  7. wpf拖拽

    简单拖拽的实现是,实现源控件的MouseDown事件,和目标控件Drop事件.调用DragDrop.DoDragDrop()以启动拖放操作,DragDrop.DoDragDrop()函数接受三个参数: ...

  8. Spring Boot 最简单的HelloWorld

    创建一个Spring Boot,可以直接使用构建工具(Maven或Gradle)创建,也可以使用spring.io网站创建,一般会选择使用spring.io创建 使用IDEA创建一个Spring Bo ...

  9. Axure RP 的安装与卸载

    官网:http://www.axure.com/download 支持Windows和Mac

  10. Go语言【第一篇】:Go初识

    Go语言特色 简洁.快速.安全 并行.有趣.开源 内存管理.数据安全.编译迅速 Go语言用途 Go语言被设计成一门应用于搭载Web服务器,存储集群或类似用途的巨型中央服务器的系统编程语言.对于高性能分 ...