POJ 1276 Cash Machine

来源:互联网 发布:淘宝直通车助手官网 编辑:程序博客网 时间:2024/06/06 02:40
Cash Machine
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 32571 Accepted: 11789

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 350633 4  500 30  6 100  1 5  0 1735 00 3  10 100  10 50  10 10

Sample Output

73563000

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.

题意:给你多张面值不同的钱币和期望达到的金钱总数,让你求在不大于金钱总数的情况下最接近金钱总数的分配方案。

其实就是一个多重背包的问题。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;struct node{    int shu;    int zhi;}q[112345];int dp[112345];int main(){    int sum,n;    while(cin>>sum>>n)    {        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;i++) scanf("%d%d",&q[i].shu,&q[i].zhi);        dp[0]=1;        int m=0;        for(int i=1;i<=n;i++)        for(int j=m;j>=0;j--)        {            if(dp[j])            {                for(int k=1;k<=q[i].shu;k++)                {                    int t=j+k*q[i].zhi;                    if(t>sum) continue;                    dp[t]=1;                    if(m<t) m=t;                }            }        }        cout<<m<<endl;    }    return 0;}


0 0
原创粉丝点击