USACO Section 3.3 Shopping Offers(dp)

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

题意:现在商场正在打折,打折的方式是同时买规定的的物品会比分开买便宜,现在告诉你打折的方式和要买的物品,求最少的钱,有个条件是不可以多买物品。

分析:d[a1][a2][a3][a4][a5] 表示第 1 个物品买 a1 个,第 2 个物品买 a2 个,第 3 个物品买 a3 个,第 4 个物品买 a4 个,第 5 个物品买 a5 个的最小值。d[a1][a2][a3][a4][a5]=min(d[a1-b1][ a2-b2][ a3-b3][ a4-b4][ a5-b5] + reduce[i] );b1、b2、b3、b4、b5 表示打折要买的物品数目。

小技巧:映射关系,p[i][j] 表示第 i 个打折方式,购买物品 j 的数量。

 

View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: shopping
*/
#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

int d[6][6][6][6][6],p[100][1000],num[100],reduce[100];
struct product
{
    int c,k,r;
}a[6];

bool cmp(product A,product B)
{
    if(A.c<B.c) return true;
    return false;
}

int main()
{
    freopen("shopping.in","r",stdin);
    freopen("shopping.out","w",stdout);
    int s,n,i,j,a1,a2,a3,a4,a5,b1,b2,b3,b4,b5;
    while(scanf("%d",&s)==1)
    {
        memset(p,0,sizeof(p));
        for(i=0;i<6;i++) a[i].c=a[i].k=0;
        for(i=1;i<=s;i++)
        {
            scanf("%d",&num[i]);
            for(j=1;j<=num[i];j++)
            {
                scanf("%d%d",&a1,&a2);
                p[i][a1]=a2;
            }
            scanf("%d",&reduce[i]);
        }
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d",&a[i].c,&a[i].k,&a[i].r);
        }
        d[0][0][0][0][0]=0;
        for(a1=0;a1<=a[1].k;a1++)
        for(a2=0;a2<=a[2].k;a2++)
        for(a3=0;a3<=a[3].k;a3++)
        for(a4=0;a4<=a[4].k;a4++)
        for(a5=0;a5<=a[5].k;a5++)
        {
            d[a1][a2][a3][a4][a5]=a1*a[1].r+a2*a[2].r+a3*a[3].r+a4*a[4].r+a5*a[5].r;
            for(i=1;i<=s;i++)
            {
                if(num[i]>n) continue;
                b1=a1-p[i][a[1].c];b2=a2-p[i][a[2].c];b3=a3-p[i][a[3].c];
                b4=a4-p[i][a[4].c];b5=a5-p[i][a[5].c];
                if(b1<0||b2<0||b3<0||b4<0||b5<0) continue;
                d[a1][a2][a3][a4][a5]=min(d[a1][a2][a3][a4][a5],d[b1][b2][b3][b4][b5]+reduce[i]);
            }
        }
        printf("%d\n",d[a[1].k][a[2].k][a[3].k][a[4].k][a[5].k]);
    }
    return 0;
}

 

你可能感兴趣的:(USACO Section 3.3 Shopping Offers(dp))