USACO-Section 3.3-PROB Shopping Offers

Shopping Offers
IOI'95

In a certain shop, each kind of product has an integer price. For example, the price of a flower is 2 zorkmids (z) and the price of a vase is 5z. In order to attract more customers, the shop introduces some special offers.

A special offer consists of one or more product items together for a reduced price, also an integer. Examples:

  • three flowers for 5z instead of 6z, or
  • two vases together with one flower for 10z instead of 12z.

Write a program that calculates the price a customer has to pay for a purchase, making optimal use of the special offers to make the price 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 14z: two vases and one flower for the reduced price of 10z and two flowers for the regular price of 4z.

PROGRAM NAME: shopping

INPUT FORMAT

The input file has a set of offers followed by a purchase.

Line 1:s, the number of special offers, (0 <= s <= 99).
Line 2..s+1:Each line describes an offer using several integers. The first integer is n (1 <= n <= 5), the number of products that are offered. The subsequent n pairs of integers c and k indicate that k items (1 <= k <= 5) with product code c (1 <= c <= 999) are part of 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.
Line s+2:The first line contains the number b (0 <= b <= 5) of different kinds of products to be purchased.
Line s+3..s+b+2:Each of the subsequent 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 to be purchased (1 <= k <= 5). The value p is the regular price per item (1 <= p <= 999). At most 5*5=25 items can be in the basket.

SAMPLE INPUT (file shopping.in)

2
1 7 3 5
2 7 1 8 2 10
2
7 3 2
8 2 5

OUTPUT FORMAT

A single line with one integer: the lowest possible price to be paid for the purchases.

SAMPLE OUTPUT (file shopping.out)

14
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define name "shopping"
using namespace std;
struct product
{
	int c,k,p;
}pd[6];
int s[100][1000];//表示第i个打折方式,购买物品j的数量。
int sp[100],x[100];
int dp[6][6][6][6][6];
int n,m;
int main()
{
	freopen(name ".in","r",stdin);
	freopen(name ".out","w",stdout);
	dp[0][0][0][0][0]=0;
	cin>>m;int i,j,a,b,l,u,v;
	for (i=1;i<=m;i++)
	{
		cin>>x[i];
		for (j=1;j<=x[i];j++)
		{
			scanf("%d%d",&a,&b);
			s[i][a]=b;
		}
		cin>>sp[i];
	}
	cin>>n;
	for (i=1;i<=n;i++)
	{
		cin>>pd[i].c;
		scanf("%d%d",&pd[i].k,&pd[i].p);
	}
	for (i=0;i<=pd[1].k;i++)
	for (j=0;j<=pd[2].k;j++)
	for (l=0;l<=pd[3].k;l++)
	for (u=0;u<=pd[4].k;u++)
    for (v=0;v<=pd[5].k;v++)
    {
    	dp[i][j][l][u][v]=i*pd[1].p+j*pd[2].p+l*pd[3].p+u*pd[4].p+v*pd[5].p;
    	for (int y=1;y<=m;y++)
    	{
    		if (x[y]>n) continue;
    		int b1=i-s[y][pd[1].c],b2=j-s[y][pd[2].c];
    		int b3=l-s[y][pd[3].c],b4=u-s[y][pd[4].c];
    		int b5=v-s[y][pd[5].c];
    		if(b1<0||b2<0||b3<0||b4<0||b5<0) continue;
            dp[i][j][l][u][v]=min(dp[i][j][l][u][v],dp[b1][b2][b3][b4][b5]+sp[y]);
    	}
    }
    cout<<dp[pd[1].k][pd[2].k][pd[3].k][pd[4].k][pd[5].k]<<endl;
    return 0;
}


只有五个商品,直接五个循环表示个数

五维dp   dp[a1][a2][a3][a4][a5]为买a1件物品1,a2件物品2,a3件物品3,a4件物品4,a5件物品5时,所需的最少价格

注意存储时的对应关系

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值