POJ - 1276 Cash Machine 多重背包 二进制优化模板

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.

假设   7     那么我们知道 要组合成1~7 的每个数    如果每个数最多取一次,那么 1 2 4 就行    

那么 1~10 呢        显然   1 2 4 3  就行

也就是说  一个数 可以拆成   lon  x  上取整   个数    

一个物品 有10个   如果 我要  取  6个  等价那么只要 取 2个的和4个   

要去取  8个 等价只要 取  1 个4个 3个的  (注意每个数只能取一次) 

如果还不理解  再举个例子 

n =3     

物品体积        3     4     5    

物品 数量      1       2     10     

二进制优化后

物品 体积    3     4*1    4*1   5*1    5*2     5*4     5*3

物品 数量     1    1          1       1     1         1        1

然后用01背包求解就可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int len=1e5+5;
const ll mod=2147483648;
const double pi=acos(-1.0);
struct Node{
	int v,c;
}p[len*15];
int dp[len];
int main()
{	
	int n,m;
	while(~scanf("%d%d",&m,&n))
	{
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		int l=0;
		for(int i=1;i<=n;++i)
		{
			int v,c;
			scanf("%d%d",&c,&v);
			for(int j=1;c-j>=0;j*=2)
			{
				p[++l].v=v*j;
				p[l].c=j;
				c-=j;
			}
			if(c>0)
			{
				p[++l].v=v*c;
				p[l].c=c;
			}
		}
		for(int i=1;i<=l;++i)
			for(int j=m;j>=p[i].v;--j)
				dp[j]|=dp[j-p[i].v];
		for(int i=m;i>=0;--i)
			if(dp[i])
			{
				printf("%d\n",i);
				break;
			}
	}
}


猜你喜欢

转载自blog.csdn.net/hutwuguangrong/article/details/86649341