POJ 1170 - Shopping Offers

28 篇文章 0 订阅

Description

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


题目大意是说有这么几种商品,有这么几种优惠,输入客户需要购买的各种商品的数量,让我们找出最优解情况下的值。

是一道动态规划,用搜索也能用。搜索方法戳这里

因为是最多购买5种商品,所以用5重循环来DP,再用一个循环来控制优惠的方法数量就好了。具体代码看下面。

#include <cstdio>
#include <cstring>

int id[6];          ///商品的编号
int num[6];         ///要购买的商品的数量(此处1-5是id数组的下标)
int pri[6];         ///商品的单价
int s_num[100][6];  ///每种优惠的商品有多少个
int s_cnt[100];     ///优惠的数量
int s_pri[100];     ///优惠的价格
int m, n;
int ans[6][6][6][6][6];

int Decode(int c)
{
    int i;
    for (i = 1; i <= 5; ++i)
    {
        if (id[i] == c)
            break;
    }
    return i;
}

int main()
{
    int p, c, index;

    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d%d%d", &id[i], &num[i], &pri[i]);
    }
    scanf("%d", &m);
    for (int i = 1; i <= m; ++i)
    {
        scanf("%d", &s_cnt[i]);
        for (int j = 1; j <= s_cnt[i]; ++j)
        {
            scanf("%d%d", &c, &p);
            index = Decode(c);
            s_num[i][index] = p;
        }
        scanf("%d", &s_pri[i]);
    }

    memset(ans, -1, sizeof(ans));
    ans[0][0][0][0][0] = 0;
    for (int i = 0; i <= num[1]; ++i)
    {
        for (int j = 0; j <= num[2]; ++j)
        {
            for (int k = 0; k <= num[3]; ++k)
            {
                for (int t = 0; t <= num[4]; ++t)
                {
                    for (int p = 0; p <= num[5]; ++p)
                    {
                        int tmp1 = 1000000;
                        int tmp2 = 1000000;
                        for (int s = 1; s <= m; ++s)
                        {
                            if (i >= s_num[s][1] &&
                                j >= s_num[s][2] &&
                                k >= s_num[s][3] &&
                                t >= s_num[s][4] &&
                                p >= s_num[s][5])
                            {
                                tmp2 = ans[i - s_num[s][1]][j - s_num[s][2]][k - s_num[s][3]][t - s_num[s][4]][p - s_num[s][5]] + s_pri[s];
                                if (tmp2 < tmp1)
                                    tmp1 = tmp2;
                            }
                        }
                        if (tmp1 != 1000000)
                            ans[i][j][k][t][p] = tmp1;
                        else
                            ans[i][j][k][t][p] = i*pri[1] + j*pri[2] + k*pri[3] + t*pri[4] + p*pri[5];
                    }
                }
            }
        }
    }
    printf("%d\n", ans[num[1]][num[2]][num[3]][num[4]][num[5]]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值