(多重背包)poj 1276


Cash Machine

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28364   Accepted: 10125

Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example,

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10

means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each.

Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine.

Notes: 
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc.

Input

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format:

cash N n1 D1 n2 D2 ... nN DN

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct.

Output

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below.

Sample Input

735 3  4 125  6 5  3 350
633 4  500 30  6 100  1 5  0 1
735 0
0 3  10 100  10 50  10 10

Sample Output

735
630
0
0

Hint

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash.

In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash.

In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.

Source

Southeastern Europe 2002

加了一个计数器而已

#include <iostream>

using namespace std;

#define MAXN 10
#define MAXCASH 100000

int main() {
	int i, j;
	int N;
	int cash;
	while (cin >> cash >> N) {
		int n[MAXN + 1], w[MAXN + 1], c[MAXN + 1], dp[MAXCASH + 1], count[MAXCASH + 1];

		for (i = 1; i <= N; i++) {
			cin >> n[i] >> w[i];
			c[i] = w[i];
		}

		memset(dp, 0, sizeof(dp));
		for (i = 1; i <= N; i++) {
			memset(count, 0, sizeof(count));
			for (j = w[i]; j <= cash; j++)
				if (dp[j] < dp[j - c[i]] + w[i] && count[j - c[i]] < n[i]) {
					dp[j] = dp[j - c[i]] + w[i];
					count[j] = count[j - c[i]] + 1;
				}
		}
		cout << dp[cash] << endl;
	}
}

  

时间: 2024-10-02 11:26:10

(多重背包)poj 1276的相关文章

多重背包——POJ 1742

对应POJ题目:点击打开链接 Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 30387   Accepted: 10325 Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found

poj 1276 Cash Machine (多重背包)

链接:poj 1276 题意:已知金额cash,给定几种不同面值的货币的数量及面值,求利用给定的货币可以凑成 小于等于cash的金额的最大值 分析:因为每种货币的面值及数量已知,可以将其转化为多重背包,背包的容量即为cash, 每个物品的价值及费用都为每种货币的面值. 多重背包可以转化为01背包,不过这样会超时,为了避免这样,可以转化为完全背包和二进制思想的01背包 #include<stdio.h> #include<string.h> int f[100010],v; int

POJ 1276 Cash Machine(多重背包的二进制优化)

题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了-- 后面学习了 用二进制来处理数据. 简单地介绍一下二进制优化:?(? ? ??)  假设数量是8,则可以把它看成是1,2,4,1的组合,即这4个数的组合包括了1-8的所有取值情况.这是为什么呢?将它们转换成二进制再观察一下: 1:1 2:10 4:100 1:1 二进制都只有0,1.所以1,2,4

【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】

转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面额的钱币,每种各ni张,求机器吐出小于等于所要求钱币的最大值 解析1:这题大牛都用了多重背包,不过,我一同学想出了一种就这题而言特别简单有效的方 法.(话说我就认为这题本来就不需要用到背包的,因为n的范围只到10,太小了).方法就是对钱进行遍历,看这些钱一共能组成多少面额的钱,然后从 cash向下枚

poj 1276 Cash Machine(多重背包)

Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26954   Accepted: 9533 题目大意:有各种不同面值的货币,每种面值的货币有不同的数量,请找出利用这些货币可以凑成的最接近且小于等于给定的数字cash的金额. 多重背包转0 1背包 对于第 i 种货币可能的状态为w[ i ]到cash 状态方程 dp[j]=dp[ j-c[i] ]+w[i]   (c[ i ] 表示"体积"

Cash Machine POJ - 1276 多重背包二进制优化

题意:多重背包模型  n种物品 每个m个  问背包容量下最多拿多少 这里要用二进制优化不然会超时 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn=100000+10; 6 int dp[maxn]; 7 int w[500],c[50]; 8 int main(){ 9 10 int n,m,maxnum; 11 whi

POJ 1276 Cash Machine 背包题解

典型的多重背包的应用题解. 可以使用二进制优化,也可以使用记录当前物品的方法解,速度更加快. const int MAX_CASH = 100001; const int MAX_N = 11; int tbl[MAX_CASH], nums[MAX_N], bills[MAX_N], cash, n; int bag() { if (cash <= 0 || n <= 0) return 0; memset(tbl, 0, sizeof(int) * (cash+1)); for (int

POJ 1837 Balance (多重背包计数)

Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11706   Accepted: 7305 Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It orders two arms

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