您的位置:首页 > 其它

poj 1276(多重背包)

2016-04-15 20:09 309 查看
Cash Machine

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 31525Accepted: 11333
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.
题意:一个银行的机器有n种钱币,第i种钱币的价值为ni,数量为Di,问如果给定价值为cash,利用这些钱币能够"拼凑"出不超过cash的钱币的最大价值是多少.(感觉好难表达清楚啊)
分析:每种钱币数量有不同的种数,多重背包问题.
《背包九讲》中多重背包问题的解法是将其每种物品Di分成2^0,2^1..2^(k-1),Di-2^k+1 个,
(例如,如果Di为13,则相应的k = 3,这种最多取13 件的物品应被分成系数分别为1 2 4 6 的四件物品)
利用二进制的思维,我们可以得到取任意件的物品都可以由这几个数组成.分组后,接下来就是01背包问题了.

///分析:每种钱币数量有不同的种数,多重背包问题.
///《背包九讲》中多重背包问题的解法是将其每种物品Di分成2^0,2^1..2^(k-1),Di-2^k+1 个,
///(例如,如果Di为13,则相应的k = 3,这种最多取13 件的物品应被分成系数分别为1; 2; 4; 6 的四件物品。
///利用二进制的思维,我们可以得到取任意件的物品都可以由这几个数组成.
///接下来就是01背包问题了.
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include <math.h>
#define N 105 ///因为题目中Dk<1000,2^10=1024,每件物品最多被分割成10个,n不超过10个,开100就OK
using namespace std;

int W
;
int dp[100050];
int main()
{
int cash,n,v,D;
while(scanf("%d",&cash)!=EOF){
scanf("%d",&n);
int k = 1;
for(int i=1;i<=n;i++){
scanf("%d%d",&D,&v);
int t = 1;
while(D>0){
if(D>=t){
W[k] = v*t;
D = D-t;
t = t<<1;
}else{          ///没法划分成2进制了
W[k] = v*D;
D=0;
}
//printf("%d\n",W[k]);
k++;
}
}
k--;
/*for(int i=1;i<=k;i++){
printf("%d\n",W[i]);
}*/
memset(dp,0,sizeof(dp));
for(int i=1;i<=k;i++){
for(int v = cash;v>=W[i];v--){
dp[v] = max(dp[v],dp[v-W[i]]+W[i]);
}
}
printf("%d\n",dp[cash]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: