Piggy-Bank

TimeLimit: 2000/1000 MS (Java/Others)  MemoryLimit: 65536/32768 K (Java/Others)
64-bit integer IO format:%I64d
 
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.". 
SampleInput
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
SampleOutput
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. 思路:裸的完全背包
 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn = ;
int weight[maxn], value[maxn];
int dp[][];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
memset(weight,,sizeof(weight));
memset(value,,sizeof(value));
memset(dp,INF,sizeof(dp));
int v1,v2,v;
scanf("%d%d",&v1,&v2);
v=v2-v1;
int m;
scanf("%d",&m);
for(int i=; i<=m; i++)
scanf("%d%d",&value[i],&weight[i]);
dp[][]=;
for(int i=; i<=m; i++)
{
for(int j=; j<=v; j++)
dp[i][j]=dp[i-][j];
for(int j=weight[i]; j<=v; j++)
dp[i][j]=min(dp[i][j], dp[i][j-weight[i]] + value[i] ) ; }
if(dp[m][v]!=INF)
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[m][v]);
else
printf("This is impossible.\n");
}
return ;
}

背包形动态规划 fjutoj1380 Piggy-Bank的更多相关文章

  1. 背包形动态规划 fjutoj2347 采药

    采药 TimeLimit:1000MS  MemoryLimit:128MB 64-bit integer IO format:%lld   Problem Description 辰辰是个天资聪颖的 ...

  2. 背包形动态规划 fjutoj2375 金明的预算方案

    金明的预算方案 TimeLimit:1000MS  MemoryLimit:128MB 64-bit integer IO format:%lld   Problem Description 金明今天 ...

  3. CJOJ 2040 【一本通】分组背包(动态规划)

    CJOJ 2040 [一本通]分组背包(动态规划) Description 一个旅行者有一个最多能用V公斤的背包,现在有n件物品,它们的重量分别是W1,W2,...,Wn,它们的价值分别为C1,C2, ...

  4. CJOJ 2307 【一本通】完全背包(动态规划)

    CJOJ 2307 [一本通]完全背包(动态规划) Description 设有n种物品,每种物品有一个重量及一个价值.但每种物品的数量是无限的,同时有一个背包,最大载重量为M,今从n种物品中选取若干 ...

  5. 【BZOJ5302】[HAOI2018]奇怪的背包(动态规划,容斥原理)

    [BZOJ5302][HAOI2018]奇怪的背包(动态规划,容斥原理) 题面 BZOJ 洛谷 题解 为啥泥萌做法和我都不一样啊 一个重量为\(V_i\)的物品,可以放出所有\(gcd(V_i,P)\ ...

  6. nyist oj 311 全然背包 (动态规划经典题)

    全然背包 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用.第i种物品的体积是c,价值是 ...

  7. 【Python】0/1背包、动态规划

    0/1背包问题:在能承受一定重量的背包中,放入重量不同,价值不同的几件物品,怎样放能让背包中物品的价值最大? 比如,有三件物品重量w,价值v分别是 w=[5,3,2] v=[9,7,8] 包的容量是5 ...

  8. 【背包型动态规划】灵魂分流药剂(soultap) 解题报告

    问题来源 BYVoid魔兽世界模拟赛 [问题描述] 皇家炼金师赫布瑞姆刚刚发明了一种用来折磨一切生物的新产品,灵魂分流药剂.灵魂分流药剂的妙处在于能够给服用者带来巨大的痛苦,但是却不会让服用者死去,而 ...

  9. 0-1背包的动态规划算法,部分背包的贪心算法和DP算法------算法导论

    一.问题描述 0-1背包问题,部分背包问题.分别实现0-1背包的DP算法,部分背包的贪心算法和DP算法. 二.算法原理 (1)0-1背包的DP算法 0-1背包问题:有n件物品和一个容量为W的背包.第i ...

随机推荐

  1. 【Spring】org.springframework.web.context.ContextLoaderListen 报错

    详细信息如下: org.apache.catalina.core.StandardContext.listenerStart Error configuring application listene ...

  2. maven私服nexus上传第三方jar包以及下载

    私服是一个特殊的远程仓库,它是架设在局域网内的仓库服务.私服代理广域网上的远程仓库,供局域网内的Maven用户使用.当Maven需要下载构建的使用,它先从私服请求,如果私服上没有的话,则从外部的远程仓 ...

  3. Spring中FactoryBean的作用和实现原理

    BeanFactory与FactoryBean,相信很多刚翻看Spring源码的同学跟我一样很好奇这俩货怎么长得这么像,分别都是干啥用的.BeanFactory是Spring中Bean工厂的顶层接口, ...

  4. 用python实现九九乘法表输出-两种方法

    2019-08-05 思考过程:九九乘法表需要两层循环,暂且称之为内循环和外循环,因此需要写双层循环来实现. 循环有for和while两种方式. for循环的实现 for i in range(1,1 ...

  5. Linux命令- echo、grep 、重定向、1>&2、2>&1的介绍

    最近笔试遇到一道题,关于Linux命令的,题目如下 下面两条命令分别会有怎样的输出 echo  hello 1>&2 |grep aaa echo  hello 2>&1 ...

  6. RocketMQ中Broker的刷盘源码分析

    上一篇博客的最后简单提了下CommitLog的刷盘  [RocketMQ中Broker的消息存储源码分析] (这篇博客和上一篇有很大的联系) Broker的CommitLog刷盘会启动一个线程,不停地 ...

  7. Redhat 离线安装 Docker (Community from binaries)

    需求 在离线环境安装Docker (Community版),因为Enterprise版要花钱.当然资金充裕的客户可参考https://docs.docker.com/install/linux/doc ...

  8. 使用webstorm搭建vue-cli项目

    前言 随着vue在前端不断的壮大,越来越多的前端工程师使用vue了,作为大型项目的开发,vue-cli是不二之选,所以这篇博客是为搭建vue-cli所写,想要搭建vue-cli项目就必须先有git,n ...

  9. kpm字符串匹配算法

    首先是简单的朴素匹配算法 /* * 返回子串t在主串s的位置,若不存在则返回0 */ public static int index(String s, String t) { int i = 0;/ ...

  10. Powered by .NET Core 进展:第5次发布尝试(Windows部署)

    (图注:Windows 自带的性能监控,红色表示 CPU 占用,绿色表示 QPS) 今天中午 12:30 左右,我们进行了 .NET Core 博客站点的第5次发布(页脚会显示"Powere ...