首页 > 其他 > 详细

HDU 2844 Coins (多重背包)

时间:2014-07-17 20:16:35      阅读:395      评论:0      收藏:0      [点我收藏+]



题意:有n种面值的硬币a[i],每种硬币有c[i]个,问能组成不大于m面值(1~m)的个数。


多重背包模板:by背包九讲。



#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define eps 1e-8
#define LL long long
#define maxn 100001
#define mol 1000000007
//procedure MultiplePack(cost,weight,amount)
//     if cost*amount>=V
//         CompletePack(cost,weight)
//         return
//     integer k=1
//     while k<amount
//         ZeroOnePack(k*cost,k*weight)
//         amount=amount-k
//         k=k*2
//     ZeroOnePack(amount*cost,amount*weight)
int n,m,a[110],c[110];
int dp[maxn];
void CompletePack(int cost,int weight)//完全背包
{
	for(int v=cost;v<=m;v++)
		dp[v]=max(dp[v],dp[v-cost]+weight);
}
void ZeroOnePack(int cost,int weight)//01背包
{
	for(int v=m;v>=cost;v--)
			dp[v]=max(dp[v],dp[v-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount)
{
	if(cost*amount>=m)
		 CompletePack(cost,weight);
	else 
	{
		int  k=1;
		while( k<amount)//二进制拆分
		{
			 ZeroOnePack(k*cost,k*weight);
			 amount=amount-k;
			 k=k*2;
		}
		ZeroOnePack(amount*cost,amount*weight);
	}
}
int main()
{
	
	while(scanf("%d%d",&n,&m))
	{
		if(n==0&&m==0) break;
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for(int i=1;i<=n;i++)
			scanf("%d",&c[i]);
		for(int i=1;i<=m;i++) dp[i]=0;
		for(int i=1;i<=n;i++)
			MultiplePack(a[i],a[i],c[i]);
		int ans=0;
		for(int i=1;i<=m;i++)
			if(dp[i]==i) 
				ans++;
		printf("%d\n",ans);

	}
	return 0;
}
/*
3 10
1 2 4 2 1 1
2 5
1 4 2 1
*/


HDU 2844 Coins (多重背包),布布扣,bubuko.com

HDU 2844 Coins (多重背包)

原文:http://blog.csdn.net/u012861385/article/details/37908581

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!