首页 > 其他 > 详细

POJ 1276 Cash Machine 背包题解

时间:2014-08-13 18:55:27      阅读:284      评论:0      收藏:0      [点我收藏+]

典型的多重背包的应用题解。

可以使用二进制优化,也可以使用记录当前物品的方法解,速度更加快。

const int MAX_CASH = 100001;
const int MAX_N = 11;
int tbl[MAX_CASH], nums[MAX_N], bills[MAX_N], cash, n;

int bag()
{
	if (cash <= 0 || n <= 0) return 0;
	memset(tbl, 0, sizeof(int) * (cash+1));
	for (int i = 0; i < n; i++)
	{
		tbl[0] = nums[i]+1;
		for (int j = 1; j <= cash; j++)
		{
			if (tbl[j]) tbl[j] = tbl[0];
			else if (j >= bills[i] && tbl[j-bills[i]] > 1)
				tbl[j] = tbl[j-bills[i]]-1;
		}
	}
	for (; !tbl[cash]; cash--);
	return cash;
}

int main()
{
	while (~scanf("%d", &cash))
	{
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
		{
			scanf("%d %d", &nums[i], &bills[i]);
		}
		printf("%d\n", bag());
	}
	return 0;
}


POJ 1276 Cash Machine 背包题解,布布扣,bubuko.com

POJ 1276 Cash Machine 背包题解

原文:http://blog.csdn.net/kenden23/article/details/38539073

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