POJ1170-Shopping Offers(状态压缩)

在这里插入图片描述
In a shop each kind of product has a price. For example, the price of a flower is 2 ICU (Informatics Currency Units) and the price of a vase is 5 ICU. In order to attract more customers, the shop introduces some special offers.
A special offer consists of one or more product items for a reduced price. Examples: three flowers for 5 ICU instead of 6, or two vases together with one flower for 10 ICU instead of 12.
Write a program that calculates the price a customer has to pay for certain items, making optimal use of the special offers. That is, the price should be as low as possible. You are not allowed to add items, even if that would lower the price.
For the prices and offers given above, the (lowest) price for three flowers and two vases is 14 ICU: two vases and one flower for the reduced price of 10 ICU and two flowers for the regular price of 4 ICU.
Input
Your program is to read from standard input. The first line contains the number b of different kinds of products in the basket (0 <= b <= 5). Each of the next b lines contains three values c, k, and p. The value c is the (unique) product code (1 <= c <= 999). The value k indicates how many items of this product are in the basket (1 <= k <= 5). The value p is the regular price per item (1 <= p <= 999). Notice that all together at most 5*5=25 items can be in the basket. The b+2nd line contains the number s of special offers (0 <= s <= 99). Each of the next s lines describes one offer by giving its structure and its reduced price. The first number n on such a line is the number of different kinds of products that are part of the offer (1 <= n <= 5). The next n pairs of numbers (c,k) indicate that k items (1 <= k <= 5) with product code c (1 <= c <= 999) are involved in the offer. The last number p on the line stands for the reduced price (1 <= p <= 9999). The reduced price of an offer is less than the sum of the regular prices.
Output
Your program is to write to standard output. Output one line with the lowest possible price to be paid.
Sample Input
2
7 3 2
8 2 5
2
1 7 3 5
2 7 1 8 2 10
Sample Output
14

分析:

题意:
第行输入数字n,表示物品的种类,然后接下来是n行输入,每行输入id,num,price三个整数,分别是物品的id,数量以及单价;n行输入后是一个整数m,表示有m种优惠组合,然后是m行,每行首先输入一个数字k,表示这种优惠方式有几种物品,然后是2k+1个整数,第2i-1和第2i个输入分别表示这种优惠的第i种物品的id和数量,最后第2k+1个输入是这种组合方式的价格;问买下所有的商品最少花多少钱?

解析:
网上有两种方法:
(1)六重循环,因为种类和数量都很小,所以这种方案可行,但是本人觉得六重循环写着比较头大,所以用了第二种
(2)状态压缩:用六进制的方法记录每种物品的体积:
如:假设有三种物品,则:
第一种物品的体积为:1num1;
第二种物品的体积为:6
num2;
第三种物品的体积为:36*num3;
这样我们就把每种物品区分开了,为什么是六进制?因为这里每种物品最多有5个,如果一个组合的体积是8,那么定然是一个第二种物品加两个第一种物品的组合;鉴于这种方式使得每种优惠方式都有唯一的表示方式,所以就转化为了完全背包的问题!

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
#define N 1005

using namespace std;

struct node{
	int num,val;
};

node book[N];
int dp[100000];
int map[N];
int six[10];

int main()
{
	int n,id,m,num,val,sum=0,Max=0;
	ios::sync_with_stdio(false);
	six[0]=1;
	for(int i=1;i<=6;i++)
	{
		six[i]=six[i-1]*6;
	}
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>id>>num>>val;
		map[id]=i-1;
		sum+=num*val;
		Max+=num*six[i-1];
		book[i].num=six[i-1];
		book[i].val=val;
	}
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		int k;
		cin>>k;
		book[i+n].num=0;
		while(k--)
		{
			cin>>id>>num;
			book[i+n].num+=six[map[id]]*num;
		}
		cin>>book[i+n].val;
	}
	memset(dp,INF,sizeof(dp));
	dp[0]=0;
	for(int i=1;i<=n+m;i++)
	{
		for(int j=book[i].num;j<=Max;j++)
		{
			dp[j]=min(dp[j],dp[j-book[i].num]+book[i].val);
		}
	}
	printf("%d\n",min(sum,dp[Max]));
	return 0;
 }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值