Chinaunix首页 | 论坛 | 博客
  • 博客访问: 343587
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-03-24 20:49:01

一、问题描述

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

二、解题思路

用动态规划方法。有一个5维数组保存状态,F[5][4][3][2][1]表示购买5个商品14个商品23个商品32个商品41个商品5的最低价格。数组O保存所有出售方案,包括单个出售的方案。

struct offer

{

       int n[6];  //n[i]保存该offer中商品i的数量

       int val;    //offer的价格

};

状态转移方程

F[n1][n2][n3][n4][n5]=min(F[n1-O[i].n[1]][n2-O[i].n[2]][n3-O[i].n[3]][n4-O[i].n[4]][n5-O[i].n[5]]+O[i].val) 变量i遍历所有方案)。

使用递归的方式自下往下搜索,计算出一个状态后便保存起来,下次不用重复计算。

三、代码

#include<iostream>
using namespace std;
struct offer
{
    int n[6];    //n[i]保存该offer中商品i的数量

    int val;    //该offer的价格

};
offer O[110];
int code[1001];
int F[6][6][6][6][6];
int s;
int GetMinCost(int n1,int n2,int n3,int n4,int n5)
{
    if(F[n1][n2][n3][n4][n5]!=-1)
        return F[n1][n2][n3][n4][n5];
    int minVal=0xffff;
    for(int i=1;i<=s;++i)
    {
        if(O[i].n[1]<=n1 && O[i].n[2]<=n2 && O[i].n[3]<=n3 && O[i].n[4]<=n4 && O[i].n[5]<=n5)
        {
            int temp=O[i].val+GetMinCost(n1-O[i].n[1],n2-O[i].n[2],n3-O[i].n[3],n4-O[i].n[4],n5-O[i].n[5]);
            if(minVal> temp)
                minVal=temp;
        }
    }
    F[n1][n2][n3][n4][n5]=minVal;
    return minVal;
}
int main()
{
    int b;
    int i,j;
    int n[6];
    
    memset(F,-1,sizeof(int)*7776);
    memset(O,0,sizeof(offer)*110);
    memset(n,0,sizeof(int)*6);
    F[0][0][0][0][0]=0;
    cin>>b;//商品数量

    for(i=1;i<=b;++i)
    {
        int c;
        int p;
        cin>>c; //商品编码

        cin>>n[i]; //篮子里该商品数量

        cin>>p; //单个该商品价格

        code[c]=i; //将商品编码映射成下标

        O[i].n[i]=1;
        O[i].val=p;
    }
    cin>>s; //special offers的数量

    s=s+b; //所有卖出方案

    for(;i<=s;++i)
    {
        int m;
        cin>>m;
        for( j=1;j<=m;++j)
        {
            int cd; //商品编码

            int num; //商品数量

            
            cin>>cd;
            cin>>num;
            O[i].n[code[cd]]=num;
        }
        int value; //价钱

        cin>>value;
        O[i].val=value;
    }
    cout<<GetMinCost(n[1],n[2],n[3],n[4],n[5])<<endl;
    return 0;
}


阅读(1026) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~