多重背包问题的实现
2018-03-11 19:26
148 查看
POJ1276
之前看过一遍背包九讲,没有深刻体会,今天碰到了多重背包的模板题=。=
Problem:Cash Machine
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 machin
4000
e delivers no cash.
PS:原题中好像没有写停止条件,我自己设置cash为-1时,程序退出。
之前看过一遍背包九讲,没有深刻体会,今天碰到了多重背包的模板题=。=
Problem:Cash Machine
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 machin
4000
e delivers no cash.
#include <iostream> using namespace std; const int N=100000; int n[10],d[10]; int dp[N+1]; int cash; void zeroOnePack(int v,int weight){ for(int i=cash;i>=v;--i){ dp[i]=max(dp[i],dp[i-v]+weight); } } void compeletePack(int v,int weight){ for(int i=v;i<=cash;++i){ dp[i]=max(dp[i],dp[i-v]+weight); } } void multiPack(int v,int weight,int amount){ if(amount*v>=cash) compeletePack(v,weight); int k=1; int num = amount>>1; while(k<=num){ zeroOnePack(k*v,k*weight); amount -= k; k<<=1; } zeroOnePack(amount*v,amount*weight); } int main() { int num; while(1){ cin>>cash; if(cash==-1) break; cin>>num; for(int i=0;i<num;++i) cin>>n[i]>>d[i]; if(num==0||cash==0){ cout<<"0"<<endl; continue; } for(int i=0;i<num;++i){ multiPack(d[i],d[i],n[i]); } cout<<dp[cash]<<endl; } return 0; }
PS:原题中好像没有写停止条件,我自己设置cash为-1时,程序退出。
相关文章推荐
- 背包问题非递归实现
- 链栈代码实现及背包问题解决
- 0-1背包问题递归和非递归实现
- 贪心法和回溯法 求解“背包、0/1背包问题”——Java 实现
- 0/1背包问题实现
- 贪心算法运用于背包问题(C++实现)
- 用贪心算法实现的背包问题(java语言)
- 0/1背包问题的动态规划法求解 —— Java 实现
- 分支限界法实现背包问题
- 背包问题-贪心法-java实现
- 简单的背包问题--java递归实现
- 背包问题——“01背包”详解及实现(包含背包中具体物品的求解)
- 背包问题——“完全背包”详解及实现(包含背包具体物品的求解)
- dynamic programming --knapsack problem 背包问题(c++实现)
- 0-1背包问题经典算法(递归实现)
- 递归实现背包问题
- 完全背包问题(Java实现)
- 背包问题-java实现
- 简单背包(无价值总和)问题的实现。
- 0-1背包问题经典算法(一维数组实现)