码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1276 Cash Machine 背包题解

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

标签:使用   for   amp   size   应用   on   poj   return   

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

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

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 背包题解

标签:使用   for   amp   size   应用   on   poj   return   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!