Hrbust 2099/Poj 1170 Shopping Offers【离散化+Dp】

Shopping Offers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5114   Accepted: 2138

Description

Hrbust 2099/Poj 1170 Shopping Offers【离散化+Dp】_第1张图片

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

Source

IOI 1995

题目大意:

有N个物品,每个物品三个元素:商品号,商品个数,每个物品的单价。

有M个组合,每个组合首先输入一个数Mi表示这个组合要有多少个物品参与进来,接下来Mi对数(C,K)表示商品号为C的物品有K个.最后一个数表示一起这样购买这Mi组物品的价格会变成的价值val.

问将N个物品都购买了,最小花费是多少。


思路:


题目不难,首先观察到数据范围都不大,那么我们考虑暴力dp,首先将物品按照编号离散化一下。

然后:

设定Dp【i】【j】【k】【l】【z】表示第1种物品购买了i个,第2种物品购买了j个,第3种物品购买了k,个第4种物品购买了l个,第5种物品购买了z个的最小花费。

那么状态转移方程我们有:
这里del【x】【j】表示第x种促销方式中,第j种物品需要购买的数量。

Hrbust 2099/Poj 1170 Shopping Offers【离散化+Dp】_第2张图片


注意一些细节以及数组越界以及初始化的问题即可。


Ac代码:

#include
#include
#include
using namespace std;
struct node
{
    int pos,num,val;
}a[10];
int del[150][15];
int delval[100];
int dp[7][7][7][7][7];
int n,m;
void init()
{
    memset(dp,0,sizeof(dp));
    memset(delval,0,sizeof(delval));
    memset(del,0,sizeof(del));
    memset(a,0,sizeof(a));
    maps;
    int tot=0;
    for(int i=1; i<=n; i++)
    {
        int p,num,val;
        scanf("%d%d%d",&p,&num,&val);
        if(s[p]==0)++tot,s[p]=tot;
        a[i].pos=s[p],a[i].num=num,a[i].val=val;
    }
    scanf("%d",&m);
    for(int j=1; j<=m; j++)
    {
        int len;scanf("%d",&len);
        for(int k=1; k<=len; k++)
        {
            int x,num;
            scanf("%d%d",&x,&num);
            del[j][s[x]]+=num;
        }
        scanf("%d",&delval[j]);
    }
    for(int i=0;i<=5;i++)
    {
        for(int j=0;j<=5;j++)
        {
            for(int k=0;k<=5;k++)
            {
                for(int l=0;l<=5;l++)
                {
                    for(int z=0;z<=5;z++)
                    {
                        dp[i][j][k][l][z]=0x3f3f3f3f;
                    }
                }
            }
        }
    }
    dp[0][0][0][0][0]=0;
}
int main()
{
    while(~scanf("%d",&n))
    {
        init();
        for(int i=0;i<=5;i++)
        {
            for(int j=0;j<=5;j++)
            {
                for(int k=0;k<=5;k++)
                {
                    for(int l=0;l<=5;l++)
                    {
                        for(int z=0;z<=5;z++)
                        {
                            if(i-1>=0)dp[i][j][k][l][z]=min(dp[i][j][k][l][z],dp[i-1][j][k][l][z]+a[1].val);
                            if(j-1>=0)dp[i][j][k][l][z]=min(dp[i][j][k][l][z],dp[i][j-1][k][l][z]+a[2].val);
                            if(k-1>=0)dp[i][j][k][l][z]=min(dp[i][j][k][l][z],dp[i][j][k-1][l][z]+a[3].val);
                            if(l-1>=0)dp[i][j][k][l][z]=min(dp[i][j][k][l][z],dp[i][j][k][l-1][z]+a[4].val);
                            if(z-1>=0)dp[i][j][k][l][z]=min(dp[i][j][k][l][z],dp[i][j][k][l][z-1]+a[5].val);
                            for(int x=1;x<=m;x++)
                            {
                                if(i>=del[x][1]&&j>=del[x][2]&&k>=del[x][3]&&l>=del[x][4]&&z>=del[x][5])
                                dp[i][j][k][l][z]=min(dp[i][j][k][l][z],dp[i-del[x][1]][j-del[x][2]][k-del[x][3]][l-del[x][4]][z-del[x][5]]+delval[x]);
                            }
                        }
                    }
                }
            }
        }
        printf("%d\n",dp[a[1].num][a[2].num][a[3].num][a[4].num][a[5].num]);
    }
}




你可能感兴趣的:(dp)