POJ1170 Shopping Offers(五维背包)

Discription
在这里插入图片描述
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

题意
一共有a种商品,每种有b个,a和b都不大于5,每个商品都可以单独卖,也有促销套装卖。把所有商品都买到的最少的钱。

思路
五维背包:dp[i5][i4][i3][i2][i1]表示五种商品分别买a,b,c,d,e个所需要花费的最少的钱。
利用两个数组,from和to存储下标和商品编码的关系。num[][]存储某种优惠政策中每种商品个数。
这个题就是各种处理非常麻烦
写五重循环,状态转移方程为:

dp[i5][i4][i3][i2][i1] = max(dp[i5][i4][i3][i2][i1],dp[i5-num[i0][5]][i4-num[i0][4]][i3-num[i0][3]][i2-num[i0][2]][i1-num[i0][1]]+sp[i0]);

AC代码

#include<cstdio>
#include<string.h>
#include<iostream>
using namespace std;
int dp[6][6][6][6][6];
int b, c, k[1000], p[1000]; 
int s; 
int to[6];
int from[1000];
int num[100][6];
int sp[100];
int n;
int main()
{
    scanf("%d", &b);
    for(int i = 1; i<=b; ++i) 
    {
        int tk, tp;
        scanf("%d%d%d", &c,&tk,&tp);
        k[c] = tk;
        p[c] = tp;
        to[i] = c;
        from[c] = i;
    }
    scanf("%d", &s);
    for(int i = 0; i<s; ++i)
    {
        scanf("%d", &n);
        int fir = 0;
        int tp;
        for(int j = 0; j<n; ++j)
        {
            int tk;
            scanf("%d%d", &c,&tk);
            num[i][from[c]] = tk;
            fir += p[c]*tk;
        }
        scanf("%d", &tp);
        sp[i] = fir - tp;
    }
    for(int i0 = 0; i0<s; ++i0) 
        for(int i5 = 0; i5<=k[to[5]]; ++i5)  
            for(int i4 = 0; i4<=k[to[4]]; ++i4)
                for(int i3 = 0; i3<=k[to[3]]; ++i3)
                    for(int i2 = 0; i2<=k[to[2]]; ++i2)
                        for(int i1 = 0; i1<=k[to[1]]; ++i1)
                            if(i5-num[i0][5]>=0 && i4-num[i0][4]>=0 && i3-num[i0][3]>=0 && i2-num[i0][2]>=0 && i1-num[i0][1]>=0)
                                dp[i5][i4][i3][i2][i1] = max(dp[i5][i4][i3][i2][i1],dp[i5-num[i0][5]][i4-num[i0][4]][i3-num[i0][3]][i2-num[i0][2]][i1-num[i0][1]]+sp[i0]);
printf("%d\n",p[to[5]]*k[to[5]]+p[to[4]]*k[to[4]]+p[to[3]]*k[to[3]]+p[to[2]]*k[to[2]]+p[to[1]]*k[to[1]]-dp[k[to[5]]][k[to[4]]][k[to[3]]][k[to[2]]][k[to[1]]]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值