POJ1170 Shopping Offers(五维背包or状压dp)

37 篇文章 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

题意:

有最多五种物品,给出每一种物品的单价和几种套餐的价格,求买目标物品至少需要多少钱

太难了!!!

想到的是五维背包,六重循环写到吐。还特别复杂。

看了大佬的代码发现是状压dp确实简单了点

状压就是每个物品的个数最多只有六种状态,可以使用六进制的方式表示每一个物品的数目。两个状态相减只有当第一种状态的每一个物品数目不小于第二种状态的数目的时候才可以相减,否则减完没有意义。 

代码如下:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[500005],HASH[1005];
int SIX[10]={1,6,36,216,1296,7776,46656,279936};
struct node{
    int has,num,val;
}s[205];
int main(){                                     //因为最多有五种物品,因此
    int b,k,m,x,y,i,j,ans,tmp,NUM;              //将每种物品和套餐用六进制
    while(scanf("%d",&b)!=EOF){                 //进行状态压缩
        ans=NUM=0;
        memset(HASH,-1,sizeof(HASH));
        for(i=0;i<b;i++){
            scanf("%d%d%d",&tmp,&s[i].num,&s[i].val);
            s[i].has=SIX[i];
            HASH[tmp]=SIX[i];
            ans+=s[i].num*s[i].val;
            NUM+=SIX[i]*s[i].num;               //将六进制总数作为背包容量
            s[i].num=SIX[i];                    //将每个物品的重量变为六进制
        }                                       //所表示的数
        scanf("%d",&m);
        for(i=0;i<m;i++){
            scanf("%d",&k);
            while(k--){
            scanf("%d%d",&x,&y);
            if(HASH[x]==-1)
            continue;
            s[i+b].num+=HASH[x]*y;              //将套餐变为六进制数
            }
            scanf("%d",&s[i+b].val);
        }
        memset(dp,INF,sizeof(dp));
        dp[0]=0;
        for(i=0;i<b+m;i++)
        for(j=s[i].num;j<=NUM;j++)              //直接转换成完全背包
        dp[j]=min(dp[j],dp[j-s[i].num]+s[i].val);
        printf("%d\n",min(ans,dp[NUM]));
    }
    return 0;
}

五维背包代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,cnt,basket[10],dp[110][10],s[110],price[1010];
int cost[10][10][10][10][10];
int main()
{
    scanf("%d",&n);
    for (int i=0;i<n;i++)
        {
        int c,num,p;
        scanf("%d %d %d",&c,&num,&p);
        price[c]=i,basket[i]=num;
        dp[cnt][i]=1;
        s[cnt++]=p;
    }
    scanf("%d", &m);
    for (int i=0;i<m;i++)
    {
        int num,c,k,p;
        scanf("%d",&num);
        for (int i=0;i<num;i++)
         {
            scanf("%d %d",&c,&k);
            dp[cnt][price[c]]=k;
        }
        scanf("%d",&p);
        s[cnt++]=p;
    }
    for(int a0=0;a0<=basket[0];a0++)
        for(int a1=0;a1<=basket[1];a1++)
            for(int a2=0;a2<=basket[2];a2++)
                for(int a3=0;a3<=basket[3];a3++)
                    for(int a4=0;a4<=basket[4];a4++)
                    {
                        if(a0==0&&a1==0&&a2==0&&a3==0&&a4==0)
                        {
                           cost[a0][a1][a2][a3][a4]=0;
                             continue;
                             }
                        cost[a0][a1][a2][a3][a4]=INF;
                        for(int i=0;i<cnt;i++)
                       {
                            if(a0<dp[i][0]||a1<dp[i][1]|a2<dp[i][2]||a3<dp[i][3]||a4<dp[i][4])
                                continue;
                            cost[a0][a1][a2][a3][a4]=min(cost[a0][a1][a2][a3][a4],cost[a0-dp[i][0]][a1-dp[i][1]][a2-dp[i][2]][a3-dp[i][3]][a4-dp[i][4]]+s[i]);
                        }
                    }
    printf("%d",cost[basket[0]][basket[1]][basket[2]][basket[3]][basket[4]]);
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值