Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35043    Accepted Submission(s): 17420

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.

题目大意:给出存钱罐的容量,和各种货币的价值的重量,然后求存钱罐里放满的最坏情况(里面钱最少)。

思路:乍一看就是完全背包,但是用贪心一发WA,然后就学习了完全背包。注意一点,这里是求最小的价值。所以初始化条件要注意一下。

 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
//head const int MAX = + ;
int w[MAX], c[MAX];
int dp[+]; //dp[i][v] 表示 前i个物品恰好放到容量为v的 最小价值 int main() {
int _, n, st, ed;
for(scanf("%d", &_);_;_--) {
scanf("%d%d", &st, &ed);
scanf("%d", &n);
for(int i = ; i < n; i++) {
scanf("%d%d", &c[i], &w[i]);
}
int V = ed - st;//容量
for(int i = ; i <= V; i++)
dp[i] = inf;// 初始化位inf 如果dp[V]为inf 那么就是没有装满
dp[] = ;//初始化~!!
for(int i = ; i < n; i++) {//核心代码
for(int v = w[i]; v <= V; v++)
dp[v] = min(dp[v], dp[v-w[i]] + c[i]);
}
if(dp[V] != inf)
printf("The minimum amount of money in the piggy-bank is %d.\n", dp[V]);
else
printf("This is impossible.\n");
}
}

kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)的更多相关文章

  1. kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)

    Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7949   Accepted: 42 ...

  2. kuangbin专题十二 POJ1661 Help Jimmy (dp)

    Help Jimmy Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14214   Accepted: 4729 Descr ...

  3. kuangbin专题十二 HDU1176 免费馅饼 (dp)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  5. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. kuangbin专题十二 HDU1260 Tickets (dp)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. kuangbin专题十二 HDU1074 Doing Homework (状压dp)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. kuangbin专题十二 HDU1087 Super Jumping! Jumping! Jumping! (LIS)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. kuangbin专题十二 HDU1069 Monkey and Banana (dp)

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. cURL使用教程及实例演示

    curl里面的函数不多,主要有: curl_init — 初始化一个CURL会话curl_setopt — 为CURL调用设置一个选项curl_exec — 执行一个CURL会话curl_close ...

  2. springmvc中针对一个controller方法配置两个url请求

    转自:https://blog.csdn.net/sun5769675/article/details/50252019

  3. Hadoop运行程序不报错只有warn也没反应也不输出结果的解决办法

    log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFact ...

  4. python的面向对象编程

    面向对象编程是一种程序的范式,它把程序看成是对不同对象的相互调用,对现实世界建立的一种模型. 面向对象编程的基本思想,类和实例.类用于定义抽象对象,实例根据类的定义被创建出来. 在python当中我们 ...

  5. [poj3348]Cows

    题目大意:求凸包面积. 解题关键:模板题,叉积求面积. 这里的cmp函数需要调试一下,虽然也对,与普通的思考方式不同. #include<cstdio> #include<cstri ...

  6. ServerSocket的建立和使用

    -------------siwuxie095 工程名:TestMyServerSocket 包名:com.siwuxie095.socket 类名:MyServerSocket.java 工程结构目 ...

  7. Swing事件机制

    -------------siwuxie095                             Swing 是基于 MVC 结构的框架     在 Swing 中,所有的用户操作都是基于 Co ...

  8. node Util 模块

    该util模块主要设计用于支持Node.js自己的内部API的需求.但是,许多实用程序对于应用程序和模块开发人员也很有用.它可以通过以下方式访问: const util = require('util ...

  9. cocos2d-js 序列帧动画

    var spriteCache = cc.spriteFrameCache;spriteCache.addSpriteFrames(res.fireworks_plist,res.fireworks_ ...

  10. 生产者与消费者-1:1-基于list

    一个生产者/一个消费者: /** * 生产者 */ public class P { private MyStack stack; public P(MyStack stack) { this.sta ...