POJ 1276.Cash Machine

 Cash Machine
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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.
 
 
 
 
 
动态规划问题

状态分析

背包问题
cash - 背包容量
D[i] - 物品的重量
n[i] - 物品的数量上限(下限为0)

答案为 dp[N][cash] (使用前N个物品,背包容量为cash时的总重量)

按照第一组样例有
dp[3][735] = dp[2][735] + dp[2][385] + dp[2][35] + dp[2][-325]
dp[2][735] = dp[1][735] + dp[1][730] + dp[1][725] + dp[1][720] + dp[1][715] + dp[1][710] + dp[1][705]
dp[1][735] = dp[0][735] + dp[0][610] + dp[0][485] + dp[0][360] + dp[0][235]
……
dp[2][385] = dp[1][385] + dp[1][380] + dp[1][375] + dp[1][370] + dp[1][365] + dp[1][360] + dp[1][355]
……
dp[2][35] = dp[1][35] + dp[1][30] + dp[1][25] + dp[1][20] + dp[1][15] + dp[1][10] + dp[1][5]
……

也即 dp[i][j] = max(dp[i-1][j-k*D[i]] + k*D[i] , dp[i][j]) (0 <= k <= D[i])

优化时间效率:
对于多重背包问题,可以采取二进制分解来优化
对于数量为0-n的物品i,可以将其分割成多个组合。
使得所有组合加起来能够等于n,并且选取一定量的组合可以组成0-n的任意数

例如13可以分解成1、2、4、6


0=0
1=1
2=2
3=1+2
4=4
5=1+4
6=2+4
7=1+2+4
8=2+6
9=1+2+6
10=4+6
11=1+4+6
12=2+4+6
13=1+2+4+6

一个数可以分成两个数,两个数相加可以得到这个数
而这两个数还能继续分成两个数
…………
如果分成的两个数相等,则可以再下次分割只分其中一个
这样在保证尽可能少分的情况下分到最深就是需要的计算方法

这样,对于一个最多有n个的物品,可以分成(近似)log(2)n个,然后对这些进行01背包求解

如果物品i的总体积大于背包体积,则不必再分割(在范围内没有上限可以看作无限)。
使用完全背包求解

因此可以将多重背包问题转化为完全背包问题和01背包问题
由于对于每一层,之前的算法循环了n[i]次,新算法循环<log n[i]次
相当于O(V*∑n[i])优化到O(V*∑log n[i])

 

AC代码:GitHub

  1 /*
  2 By:OhYee
  3 Github:OhYee
  4 Email:oyohyee@oyohyee.com
  5 Blog:http://www.cnblogs.com/ohyee/
  6 
  7 かしこいかわいい?
  8 エリーチカ!
  9 要写出来Хорошо的代码哦~
 10 */
 11 
 12 
 13 #include <cstdio>
 14 #include <algorithm>
 15 #include <cstring>
 16 #include <cmath>
 17 #include <string>
 18 #include <iostream>
 19 #include <vector>
 20 #include <list>
 21 #include <queue>
 22 #include <stack>
 23 #include <map>
 24 using namespace std;
 25 
 26 //DEBUG MODE
 27 #define debug 0
 28 
 29 //循环
 30 #define REP(n) for(int o=0;o<n;o++)
 31 
 32 /*
 33 状态分析
 34 
 35 背包问题
 36 cash - 背包容量
 37 D[i] - 物品的重量
 38 n[i] - 物品的数量上限(下限为0)
 39 
 40 答案为 dp[N][cash] (使用前N个物品,背包容量为cash时的总重量)
 41 
 42 按照第一组样例有
 43 dp[3][735] = dp[2][735] + dp[2][385] + dp[2][35] + dp[2][-325]
 44 dp[2][735] = dp[1][735] + dp[1][730] + dp[1][725] + dp[1][720] + dp[1][715] + dp[1][710] + dp[1][705]
 45 dp[1][735] = dp[0][735] + dp[0][610] + dp[0][485] + dp[0][360] + dp[0][235]
 46 ……
 47 dp[2][385] = dp[1][385] + dp[1][380] + dp[1][375] + dp[1][370] + dp[1][365] + dp[1][360] + dp[1][355]
 48 ……
 49 dp[2][35] = dp[1][35] + dp[1][30] + dp[1][25] + dp[1][20] + dp[1][15] + dp[1][10] + dp[1][5]
 50 ……
 51 
 52 也即 dp[i][j] = max(dp[i-1][j-k*D[i]] + k*D[i] , dp[i][j]) (0 <= k <= D[i])
 53 
 54 优化时间效率:
 55 对于多重背包问题,可以采取二进制分解来优化
 56 对于数量为0-n的物品i,可以将其分割成多个组合。
 57 使得所有组合加起来能够等于n,并且选取一定量的组合可以组成0-n的任意数
 58 
 59 例如13可以分解成1、2、4、6
 60     0=0
 61     1=1
 62     2=2
 63     3=1+2
 64     4=4
 65     5=1+4
 66     6=2+4
 67     7=1+2+4
 68     8=2+6
 69     9=1+2+6
 70     10=4+6
 71     11=1+4+6
 72     12=2+4+6
 73     13=1+2+4+6
 74 
 75     一个数可以分成两个数,两个数相加可以得到这个数
 76     而这两个数还能继续分成两个数
 77     …………
 78     如果分成的两个数相等,则可以再下次分割只分其中一个
 79     这样在保证尽可能少分的情况下分到最深就是需要的计算方法
 80 
 81     这样,对于一个最多有n个的物品,可以分成(近似)log(2)n个,然后对这些进行01背包求解
 82 
 83     如果物品i的总体积大于背包体积,则不必再分割(在范围内没有上限可以看作无限)。
 84     使用完全背包问题求解
 85 
 86     因此可以将多重背包问题转化为完全背包问题和01背包问题
 87     由于对于每一层,之前的算法循环了n[i]次,新算法循环<log n[i]次
 88     相当于O(V*∑n[i])优化到O(V*∑log n[i])
 89 
 90 
 91     
 92 */
 93 
 94 const int maxn = 15;
 95 const int maxv = 100001;
 96 
 97 
 98 int cash,N;
 99 int n[maxn],D[maxn];
100 
101 int dp[maxv];
102 
103 int v;
104 
105 //0-1背包问题
106 void ZeroOnePack(int cost,int weight) {
107     //printf("    ZeroOnePack cost:%d weight:%d
",cost,weight);
108     for (int i = v; i >= cost; i--)
109         dp[i] = max(dp[i],dp[i - cost] + weight);
110 }
111 
112 //完全背包问题
113 void CompletePack(int cost,int weight) {
114     //printf("    ComplatePack cost:%d weight:%d
",cost,weight);
115     for (int i = cost; i <= v; i++)
116         dp[i] = max(dp[i],dp[i - cost] + weight);
117 }
118 
119 //多重背包问题
120 void MultiplePack(int cost,int weight,int n) {
121     //printf("MultiplePack cost:%d weight:%d cnt:%d
",cost,weight,n);
122     if (cost * n > v) {
123         CompletePack(cost,weight);
124     } else {
125         int k = 1;
126         while (k < n) {
127             ZeroOnePack(cost * k,weight * k);
128             n -= k;
129             k *= 2;
130         }
131         ZeroOnePack(cost * n,weight * n);
132     }
133 }
134 
135 
136 
137 bool Do() {
138     if (scanf("%d%d",&cash,&N) == EOF)
139         return false;
140     REP(N)
141         scanf("%d%d",&n[o + 1],&D[o + 1]);
142 
143     memset(dp,0,sizeof(dp));
144 
145     v = cash;
146 
147     for (int i = 1; i <= N; i++)
148         MultiplePack(D[i],D[i],n[i]);
149 
150     printf("%d
",dp[cash]);
151 
152     return true;
153 }
154 
155 int main() {
156     while (Do());
157     return 0;
158 }
原文地址:https://www.cnblogs.com/ohyee/p/5444176.html