poj 1276

来源:互联网 发布:linux more 查找 编辑:程序博客网 时间:2024/05/17 06:54
Cash Machine
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 21568 Accepted: 7562

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
 
 
 
 
这是一道Dp中的多重背包题;
将每一个可以到达的钱币数用数组标记下来,即用DP[i]表示是否可以得到钱币数i;
至于怎样解决多重背包的问题,如下:
对每一种钱币的情况遍历一次;
其中,如果钱币数*钱币值>=k,则可以转化成完全背包的问题(从钱币值到k遍历一次);
否则,用二进制的转换,将钱币数化为几个不同的数相加,也就是将一种钱币转换成不同的几种钱币,然后当成是01背包做。
(用二进制的原因是用二进制表示后,可以保证0~钱币数可以用得到的数相加表示)
 
 
代码实现:
 
 
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int a[20];int b[20];int dp[100005];int n,m;void all(int a1,int b1){     for(int j=a1*b1;j<=m;j++){             if(dp[j]==0&&dp[j-a1*b1]==1)                 dp[j]=1;                 }}void zeroone(int a2,int b2){     for(int j=m;j>=a2*b2;j--){             if(dp[j]==0&&dp[j-a2*b2]==1){                 dp[j]=1;                 }                 }}int main(){  //  int m,n;    while(cin>>m>>n){        memset(dp,0,sizeof(dp));        dp[0]=1;        for(int i=0;i<n;i++){                cin>>a[i]>>b[i];                }        for(int i=0;i<n;i++){                if(a[i]<1)                    continue;                if(a[i]*b[i]>=m){                     all(1,b[i]);                     continue;                     }                int temp=1;                int temp2=a[i];                while(temp<temp2){                     zeroone(temp,b[i]);                     temp2=temp2-temp;                     temp=temp*2;                     }                zeroone(temp2,b[i]);                }        for(int k=m;k>=0;k--){                if(dp[k]==1){                     cout<<k<<endl;                     break;                     }                     }                     }    return 0;}

原创粉丝点击