POJ 1276 Cash Machine Multi-pack-binary optimization

Source: Internet
Author: User

Click to open link
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 was 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 have a supp Ly 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 ten 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 so can be effectively delivered according for the available bill supply of the machine.

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

Input

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

Cash N N1 D1 n2 D2 ... nN DN

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=n <= are the number of Bill denominations and 0 <= nk <= are the number of available bills for the DK denomination, 1 <= dk <=, k=1,n. White space s can occur freely between the numbers in the input. The input data is 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 Belo W.

Sample Input

735 3  4  6 5  3 350633 4 ten  6 1  5 0  1735  3

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 is not fit the exact amount of cash requested. The maximum cash that can was delivered is @630. Notice that there can is several possibilities to combine the bills of the machine for matching the delivered cash.

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

Source

Southeastern Europe 2002
There are n different denominations of currencies that tell you the face value and quantity of each currency, and find out the amount of the closest and less equal to the given number cash that can be made using these currencies.
multiple knapsack problem. can be converted to 01 backpack, then binary optimization, time complexity from O (v*σn[i]) down to O (v*σlog n[i]). See backpack nine for details.
//600k79ms#include<stdio.h> #include <string.h> #include <algorithm>using namespace Std;int dp[    100007];int Weight[10007],value[10007];int Main () {int sum,n;        while (scanf ("%d%d", &sum,&n)!=eof) {int a,b,num=0;        Memset (Dp,0,sizeof (DP));            for (int i=1;i<=n;i++)//binary optimization {scanf ("%d%d", &a,&b);                for (int j=1;j<=a;j<<=1) {value[num]=weight[num]=j*b;                num++;            A-=j;                } if (a>0) {value[num]=weight[num]=a*b;            num++; }} for (int i=0;i<num;i++) for (int j=sum;j>=weight[i];j--) Dp[j]=max (dp[j-we         IGHT[I]]+VALUE[I],DP[J]);    printf ("%d\n", Dp[sum]); } return 0;} 

Backpack Nine in the binary optimization:
524k63ms#include<stdio.h> #include <string.h> #include <algorithm>using namespace Std;int dp[ 100007],num[17],weight[17];int sum;void zeroonepack (int cost) {for (int i=sum;i>=cost;i--) Dp[i]=max (dp[i],dp[ I-cost]+cost);} void completepack (int cost) {for (int i=cost;i<=sum;i++) Dp[i]=max (dp[i],dp[i-cost]+cost);}    void Multiplepack (int count,int cost) {if (count*cost>sum) completepack (cost);        else {int k=1;            while (K<count) {zeroonepack (k*cost);            Count-=k;        k<<=1;    } zeroonepack (Count*cost);    }}int Main () {int n; while (scanf ("%d%d", &sum,&n)!=eof) {for (int i=1;i<=n;i++) scanf ("%d%d", &num[i],&we        Ight[i]);        Memset (Dp,0,sizeof (DP));        for (int i=1;i<=n;i++) Multiplepack (Num[i],weight[i]);    printf ("%d\n", Dp[sum]); }}


POJ 1276 Cash Machine Multi-pack-binary optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.