POJ Cash Machine 1276(多重背包)
2016-08-16 20:31
281 查看
[align=center]Cash Machine[/align]
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
Sample Output
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.
题意:给出一个容量,和有数量的物品,问装物品,最多能装多少
分析:这就是一个多重背包,一开始转成0,1背包,结果超时,看样得需要优化
优化算是有两种:
1 转成01背包,用二进制优化
2 转化成多重背包,可这种方法只适合体积和价值相等的情况
1 转0 1背包
2 转多重背包
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32926 | Accepted: 11929 |
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.
题意:给出一个容量,和有数量的物品,问装物品,最多能装多少
分析:这就是一个多重背包,一开始转成0,1背包,结果超时,看样得需要优化
优化算是有两种:
1 转成01背包,用二进制优化
2 转化成多重背包,可这种方法只适合体积和价值相等的情况
1 转0 1背包
63MS #include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include <stdio.h> using namespace std; int dp[112345]; int main() { int n,m,i,j; int mi,wi; int w[100000]; while(~scanf("%d",&n)) { scanf("%d",&m); memset(dp,0,sizeof(dp)); int t=0; for(i=0;i<m;i++) { int sum=1; int base=1; int ct=0; scanf("%d%d",&mi,&wi); while(sum<=mi) //二进制优化 { w[t]=base*wi; ct+=base; base*=2; sum+=base; t++; } if(ct<mi) //等价(sum-base)<mi { w[t++]=(mi-ct)*wi; } } for(i=0;i<t;i++) { for(j=n;j>=w[i];j--) { dp[j]=max(dp[j],dp[j-w[i]]+w[i]); } } printf("%d\n",dp ); } return 0; }
2 转多重背包
16MS #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int dp[112345],num[112345]; int main() { int n,m,i,j; int mi[11000],wi[11000]; while(~scanf("%d",&n)) { memset(dp,0,sizeof(dp)); scanf("%d",&m); int t=0; while(m--) { scanf("%d%d",&mi[t],&wi[t]); t++; } for(i=0;i<t;i++) { memset(num,0,sizeof(num)); for(j=wi[i];j<=n;j++) { if(dp[j-wi[i]]+wi[i] > dp[j] && num[j-wi[i]]<mi[i]) { dp[j]=dp[j-wi[i]]+wi[i]; num[j]=num[j-wi[i]]+1;//计数器+1 } } } printf("%d\n",dp ); } return 0; }
相关文章推荐
- Cash Machine POJ 1276 多重背包
- Cash Machine poj 1276(多重背包)
- Cash Machine POJ - 1276 多重背包
- Cash Machine POJ - 1276 (多重背包(模板))
- poj1276(Cash Machine + 多重背包)
- POJ 1276 Cash Machine <多重背包>
- poj&nbsp;1276&nbsp;Cash&nbsp;Machine(多重背包…
- Cash Machine POJ - 1276 (多重背包)
- POJ-1276-Cash-Machine 二进制优化多重背包问题
- poj 3267 the coe lexicon poj 1276 cash machine poj 1837 balance
- POJ POJ Cash Machine 多重背包
- poj 1276 Cash Machine
- poj 1276 Cash Machine F
- Cash Machine poj 1276
- poj 1276 Cash Machine(多重背包)
- POJ-1276-Cash Machine-多重背包
- POJ-1276 Cash Machine(多重背包问题)
- POJ 1276:Cash Machine 多重背包
- poj1276 Cash Machine-多重背包
- POJ 1276 多重背包+模板