Constraints
Time Limit: 10 secs, memory limit: 32 MB
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 shocould deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be specified tively delivered according to the available bill supply the machine.
Input
The program input is from a text file. Each data set in the file stands for a participant 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. 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 |
Sample output |
Comment |
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 |
735 630 0 0 |
735 = 1*350 + 3*125 + 2*5 630 = 6*100 + 1*30 or 21*30 No cash delivered No cash delivered |
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.
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
Question Analysis:
Multiple knapsack problems. The following describes the general solution of the knapsack problem.
Define the state with a subproblem: that is, F [I] [v] indicates the maximum value that a backpack with a capacity of V can obtain when the first I item is placed. The state transition equation is: F [I] [v] = max {f [I-1] [v], f [I-1] [V-C [I] + W [I]}. Space can be compressed, F [v] = max {f [v], F [V-C [I] + W [I]}
This equation is very important. Basically all the equations related to the backpack are derived from it. Therefore, it is necessary to explain in detail: "Put the first I items into a backpack with a capacity of V, if you only consider the I-th item Policy (put or not put), then it can be converted into a problem that only involves the previous I-1 items. If I items are not put, then the problem is converted to "pre-i-1 items into the capacity of V backpack", the value is f [I-1] [v]; if I items are placed, the problem is converted to "the previous I-1 items are placed in the backpack with the remaining capacity V-C [I ", the greatest value that can be obtained at this time is F.
[I-1] [V-C [I] plus the value of W [I] obtained by placing item I.
Note that f [v] makes sense when and only if there is a subset of the first I items, the total cost is v. Therefore, the final answer is not necessarily the maximum value of F [N] [v], but of F [N] [0. V. If you remove the "exactly" word in the state definition, you need to add another f [V-1] In the transition equation, so that f [N] [v] is the final answer. It's up to you to understand why.
# Include <iostream> # include <iomanip> # include <stdio. h> # include <cmath> # include <iomanip> # include <list> # include <map> # include <vector> # include <string> # include <algorithm> # include <sstream> # include <stack> # include <queue> # include <string. h ># include <set> using namespace STD; # define size limit 1int record [11] [size]; // indicates that the I element is obtained, int data [2] [11]; // 0 indicates number 1 indicates weight int main () {int sum; while (CIN> sum) {MEMS Et (record, 0, sizeof (record); memset (data, 0, sizeof (data); int N; CIN> N; For (INT I = 0; I <n; I ++) CIN> data [0] [I]> data [1] [I]; for (Int J = 0; j <= sum & n! = 0; j ++) record [0] [J] = min (INT) (J/data [1] [0]), data [0] [0]) * Data [1] [0]; for (INT I = 1; I <n; I ++) {for (Int J = 0; j <= sum; j ++) {int maxtmp = record [I-1] [J]; for (int K = 1; k <= data [0] [I] & K * Data [1] [I] <= J; k ++) {maxtmp = max (maxtmp, record [I-1] [J-K * Data [1] [I] + K * Data [1] [I]);} record [I] [J] = maxtmp;} // end J} If (n = 0) cout <0 <Endl; elsecout <record [n-1] [Sum] <Endl ;}}