poj 1170 Shopping Offers-动态规划-完全背包

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
题意:

顾客要买的b(<=5)种物品,每种编号为c(共有999种商品),每种有k个,单价p元,现有s种优惠组合,每种组合包含n种商品,给定n种物品的编号c及其数量k,以及组合价格p

求购买所有物品所需最小费用

输入:第一行b,接下来b行每行三个数c、k、p,之后一行一个s,接下来s行每行描述一个组合

输出:

最小费用 解题思路: 动态规划中的完全背包问题,把顾客要买 商品(含种类数量)看成 背包,每种优惠组合看成 物品 ,优惠组合中内中 商品的数量 看成 拿取每个物品的代价(如重量、大小等等),每个优惠组合省下来的钱 看成 物品的价值,由于每组合可以拿取好几次,所以是完全背包问题,这里优化了空间复杂度,只开5维数组,省去了第i个物品这个量。 比较麻烦的是顾客不一定要买几种商品(<=5),这几种商品的编号有可能很大,不可能开999维数组,所以要建立一个对应关系,这里用的是from数组和to数组,具体看代码很清楚。6重循环里第一层循环表示第i件物品,剩下5层用来控制拿取。
代码:
#include<cstdio>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
    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;
    ///输入:优惠的物品 编号 数量 编号 数量 总价格

    memset(sp,0,sizeof(sp));
    memset(dp,0,sizeof(dp));
    memset(k,0,sizeof(k));
    memset(to,0,sizeof(to));

    scanf("%d", &b);///购买的商品种类数量

   for(int i = 1; i<=b; ++i){ //从第一种商品到第b种商品。
        int tk, tp;
        scanf("%d%d%d", &c,&tk,&tp);
        k[c] = tk; p[c] = tp;//编号为c的商品的 数量,价格。
        to[i] = c;//第i种商品的编号
        from[c] = i;//这个编号是顾客要买的第几种商品
    }

    scanf("%d", &s);
    for(int i = 0; i<s; ++i){
        scanf("%d", &n);//该优惠种有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;//第i种优惠下,编号为c的物品(顾客要买的第from[c]种物品)的数量
            fir += p[c]*tk;
        }
        scanf("%d", &tp);
        sp[i] = fir - tp;
    }
    for(int i0 = 0; i0<s; ++i0){  //i0表示第几种优惠,对应 背包问题 中的 第几个物品
        for(int i5 = 0; i5<=k[to[5]]; ++i5){ // i->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",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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值