A_全然背包

/*
     copyright:   Grant Yuan
     algorithm:   全然背包
     time     :   2014.7.18
__________________________________________________________________________________________________
A - 全然背包 基础
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit

Status
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
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<cstdlib>
#define INF 999999999;
using namespace std;
long long w[501];
long long p[501];
long long dp[2][10001];
long long w1,w2;
long long t,n;
long long w3;
int main()
{
	cin>>t;
	while(t--){
		 cin>>w1>>w2;
		 w3=w2-w1;
          cin>>n;
		 for(int i=0;i<n;i++)
			cin>>p[i]>>w[i];
	     for(int i=0;i<2;i++)
			  for(int j=1;j<10001;j++)
			    dp[i][j]=INF;
		 for(int i=0;i<n;i++)
			for(int j=0;j<=w3;j++)
			  {
			  	if(j<w[i])
					  dp[(i+1)&1][j]=dp[i&1][j];
			    else
					   dp[(i+1)&1][j]=min(dp[i&1][j],dp[(i+1)&1][j-w[i]]+p[i]);
			  }
			  if(dp[n&1][w3]<999999999)
				printf("The minimum amount of money in the piggy-bank is %lld.\n",dp[n&1][w3]);
			  else
				printf("This is impossible.\n");
			  }
			  return 0;
}
时间: 2024-11-05 16:01:42

A_全然背包的相关文章

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

全然背包 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用.第i种物品的体积是c,价值是w. 求解将哪些物品装入背包可使这些物品的体积总和不超过背包容量,且价值总和最大.本题要求是背包恰好装满背包时,求出最大价值总和是多少. 假设不能恰好装满背包,输出NO 输入 第一行: N 表示有多少组測试数据(N<7). 接下来每组測试数据的第一行有两个整数M.V. M表示物品种类的数目,V表示背

UVA 10306 e-Coins(全然背包: 二维限制条件)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1247">http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1247 题意: 对于每一个例子.先给定两个数n,m,分别表示有n种硬币,对于每一种硬币有两个价值.分别记做x,y,题目要求从中

HDU1248 寒冰王座 【数学题】or【全然背包】

寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10550    Accepted Submission(s): 5355 Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,仅仅有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前. 死亡骑

HDU 1114 Piggy-Bank 全然背包

Piggy-Bank Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action come

POJ 3260 The Fewest Coins(多重背包+全然背包)

http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币,相应面值为val[1],val[2]-val[n]. 然后他身上每种货币有num[i]个. John必须付给售货员>=m的金钱, 然后售货员会用最少的货币数量找钱给John. 问你John的交易过程中, 他给售货员的货币数目+售货员找钱给他的货币数目 的和最小值是多少? 分析: 本题与POJ 1252类型: http://blog.csdn.net/u013480600

HDU 1248寒冰王座-全然背包或记忆化搜索

寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13001    Accepted Submission(s): 6620 Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,仅仅有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前. 死亡骑

白话背包之全然背包

借着前面的  白话背包之01背包 的基础,来结合图看看全然背包是个什么东东,希望以后自己看能一目了然,能对刚接触的童鞋有帮助是最好只是滴 一:关于全然背包 有N个物品,每一个物品(有无限多个) i 相应有重量w[i].价值va[i].有一个背包能够放M重的物品,如今让你从N钟物品中选择一些物品,在不超过背包上限情况使得背包装的价值最大. 二:初步了解全然背包算法 那么这里看看状态转移方程: dp[i][j]=max{dp[i-1][j-k*w[i]]+k*va[i] | 0<=k*va[i]<

01背包模板、全然背包 and 多重背包(模板)

转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/07/31/121803.html 模版就直接贴代码: 01背包模板: /* 01背包问题 01背包问题的特点是,">每种物品仅有一件.能够选择放或不放. 01背包问题描写叙述: 有N件物品和一个容量为V的背包. 第i件物品的重量是c[i],价值是w[i]. 求解将哪些物品装入背包可使这些物品

poj 1384 Piggy-Bank(全然背包)

http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7900 Accepted: 3813 Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income f