POJ-1170 Shopping offers 【状压DP】

题目描述

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

输入格式

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.

输出格式

Your program is to write to standard output. Output one line with the lowest possible price to be paid.

题意

b(0 <= b <= 5)种物品,每种有个标号c(1 <= c <= 999),有个需要购买的个数k(1 <= k <=5),有个单价p(1 <= p <= 999),有s(0 <= s <= 99)种组合优惠方案,问完成采购最少需要多少钱。

样例输入

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

我自己考虑的情况:

2
1 5 2
2 1 50
1
1 1 5 8
//out:58
2
7 3 2
8 2 50
1
1 7 3 5
//out:105

样例输出

14






题解:

  • 题目数据较小(都在5范围内),可以考虑用一个五位数表示购买的状态。比如00032表示第一种商品买2件,第二种商品买3件。
  • 如此一来可以将每个优惠组合都看成一个“单独的商品”,将问题转化成完全背包问题
  • d p [ i ] dp[i] dp[i]表示购买状态为i时消耗的最少的钱
  • 转移方程: d p [ i ] = min ⁡ ( d p [ i − s t a t [ j ] ] + p r i c e [ j ] ,     d p [ i ] ) dp[i]=\min(dp[i-stat[j]] + price[j],\ \ \ dp[i]) dp[i]=min(dp[istat[j]]+price[j],   dp[i]),其中j为所有的商品、优惠的下标
  • 其中需要注意转移状态时不能对 不合法的状态 进行转移,并且不能从 不合法的状态 转移过来,比如00701明显就是一个不合法的状态,因为每个商品最大的数量就是5,不可能有7的。同样,对于00005的一种“商品”,在对其进行转移时,不能用其对00010更新(不然的话00010是从00005转移来的,这两者含义完全不一样)这样的问题就出在状态加减的进位问题上面。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int b,s;
int m[1000]; // MAP product code TO 1..5
int staPrice[77777], dp[77777], stat[1001];
//stat存的是不同的状态(00001,00010,...)(离散化)
//staPrice[stat[i]]表示第i种状态的商品的价格
int unitPrice[5], unitmax[5]={5,5,5,5,5};
int stacnt = 0, targetSta=0;

bool isval(int i, int sta) //判断商品sta去更新i状态是否合法
{
    for(int j=0;j<b;j++,i/=10, sta/=10)
    {
        if(i%10>unitmax[j] || i%10<sta%10) return false;
    }
    return true;
}



int main()
{
    scanf("%d", &b);
    int c, k, p;
    for(int i=0;i<b;i++)
    {
        scanf("%d %d %d", &c, &k, &p);
        m[c] = i;
        unitPrice[i] = p; unitmax[i] = k;
        int sta = 1*(int)pow(10, i);
        targetSta += k * sta;
        staPrice[sta] = p;
        stat[stacnt++] = sta;
    }
    //cout << targetSta << endl;
    scanf("%d", &s);
    int n;
    for(int i=0;i<s;i++)
    {
        scanf("%d", &n);
        int sta = 0;
        while(n--)
        {
            scanf("%d %d", &c, &k);
            sta += k * (int)pow(10, m[c]);
        }
        scanf("%d", &p);
        stat[stacnt++] = sta;
        staPrice[sta] = p;
    }
    memset(dp, 0x3f, sizeof dp);
    dp[0] = 0;
    for(int i=0;i<stacnt;i++)
    {
        int packprice = staPrice[stat[i]];
        //cout << stat[i] << 'x' << packprice << endl;
        //int limtp=1;
        for(int j= stat[i];j<=targetSta;j++)
        {
            //cout << j << ' ' << isval(j);
            if(!isval(j, stat[i])) continue;
            	dp[j] = min(dp[j], dp[j-stat[i]]+packprice);
            //cout << j << ' ' << dp[j] << endl;
        }
    }
    cout << dp[targetSta] << endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值