POJ1276 Cash Machine


Language:
Default

Cash Machine

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27946   Accepted: 9973

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>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
#define maxn 100000+10

using namespace std;
int v,N;
int w[11],c[1001];
int dp[maxn];

void ZeroOnePack(int cost,int weight)
{
    for(int j=v; j>=cost; j--) ///注意是逆序的
        dp[j]=max(dp[j],dp[j-cost]+weight);
}

void CompletePack(int cost,int weight)
{
    for(int j=cost; j<=v; j++) ///注意是顺序的
        dp[j]=max(dp[j],dp[j-cost]+weight);
}

void MultiplePack(int cost,int weight,int amount)
{
    if(cost*amount>=v)
        CompletePack(cost,weight);
    else
    {
        int k=1;
        while(k<amount)
        {
            ZeroOnePack(k*cost,k*weight);
            amount-=k;
            k+=k;
        }
        ZeroOnePack(amount*cost,amount*weight);
    }
}

int main()
{
    while(~scanf("%d%d",&v,&N))
    {
        for(int i=0; i<N; i++)
            cin>>w[i]>>c[i];
        memset(dp,0,sizeof(dp));
        for(int i=0; i<N; i++)
            MultiplePack(c[i],c[i],w[i]);
        cout<<dp[v]<<endl;
    }
    return 0;
}

时间: 2024-08-01 04:27:36

POJ1276 Cash Machine的相关文章

POJ1276:Cash Machine(多重背包)

题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <math.h> using namespace std; int V,n,w

poj 1276 Cash Machine(dp 水题)

Language: Default Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27799   Accepted: 9913 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested c

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 ] 表示"体积"

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

点击打开链接 Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28337   Accepted: 10113 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount

PKU 1276 Cash Machine

<span style="color:#000099;">/* Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26604 Accepted: 9397 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bi

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 1276 Cash Machine (多重背包)

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

Cash Machine poj (多重背包)

E - Cash Machine Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amo

【POJ1276】Cash Machine

挖坑待填大神博客转载http://www.cppblog.com/MatoNo1/archive/2011/07/05/150231.aspx多重背包的单调队列初中就知道了但一直没(不会)写二进制优化初中就写过一直不写会心虚就写一下这个吧朴素方程dp[i,j]=max(dp[i-1,j-w[i]*k]+c[i]*k) w[i]*k<=j k<=j div w[i]忽略第一维dp[j]=max(dp[j-w[i]*k]+c[i]*k)复杂度O(m2n)以下是大神原文:将决策下标按照模w0的余数进