POJ - 1170 Shopping Offers (五维DP)

题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折,有s种打折方式。问怎么才能使买的价格达到最低

解题思路:最多只有五种商品,且每件商品最多只有5个,所以可以用5维dp来表示,每个维度都代表一件商品的数量
打折的方式其实有b + s种,将每种商品单件卖的也算一种打折方式
这题有个坑点,就是b或者s有可能为0

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 6
#define maxs 200
#define INF 0x3f3f3f3f
struct product{
    int c, k, p;
}pro[maxn];

struct offer{
    int num[maxn], price;
}off[maxs];
int b, s;
int dp[maxn][maxn][maxn][maxn][maxn];
map<int,int> m;
void init() {
    memset(dp, -1, sizeof(dp));
    memset(off, 0, sizeof(off));
    m.clear();
    for(int i = 0; i < b; i++) {
        scanf("%d%d%d", &pro[i].c, &pro[i].k, &pro[i].p);
        m[pro[i].c] = i;
        off[i].num[i] = 1;
        off[i].price = pro[i].p;
    }
    if(b < 5) {
        for(int i = b; i < 5; i++)
            pro[i].k = pro[i].p = 0;
    }
    scanf("%d", &s);

    int n, c, k;
    for(int i = b; i < b + s; i++) {
        scanf("%d", &n);
        for(int j = 0; j < n; j++) {
            scanf("%d%d", &c, &k);
            off[i].num[m[c]] += k;
        }
        scanf("%d", &off[i].price);
    }
}

int dfs(int a1, int a2, int a3, int a4, int a5) {
    if(dp[a1][a2][a3][a4][a5] != -1)
        return dp[a1][a2][a3][a4][a5];
    dp[a1][a2][a3][a4][a5] = INF;

    for(int i = 0; i < b + s; i++) {
        if(a1 >= off[i].num[0] && a2 >= off[i].num[1] && a3 >= off[i].num[2]  && a4 >= off[i].num[3] && a5 >= off[i].num[4])
            dp[a1][a2][a3][a4][a5] = min(dp[a1][a2][a3][a4][a5], off[i].price + dfs(a1-off[i].num[0],a2-off[i].num[1],a3-off[i].num[2],a4-off[i].num[3],a5-off[i].num[4]));
    }

    return dp[a1][a2][a3][a4][a5];
}

void solve() {
    dp[0][0][0][0][0] = 0;
    if(b == b + s) {
        int t = 0;
        for(int i = 0; i < 5; i++)
            t += pro[i].p * pro[i].k;
        printf("%d\n", t);
    }   
    else 
        printf("%d\n",dfs(pro[0].k, pro[1].k, pro[2].k, pro[3].k, pro[4].k));
}

int main() {
    scanf("%d", &b);
    init();
    solve();
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值