POJ1170 Shopping Offers <五维DP>

Problem

Shopping Offers

Description

In a shop each kind of product has a price. For example, the price of a flower is 22 ICU (Informatics Currency Units) and the price of a vase is 55 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 55 ICU instead of 66, or two vases together with one flower for 1010 ICU instead of 1212.
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 1414 ICU: two vases and one flower for the reduced price of 1010 ICU and two flowers for the regular price of 44 ICU.

Input

Your program is to read from standard input. The first line contains the number bb of different kinds of products in the basket (0b5)(0 \le b \le 5). Each of the next b lines contains three values cc, kk, and pp. The value cc is the (unique) product code (1c999)(1 \le c \le 999). The value kk indicates how many items of this product are in the basket (1k5)(1 \le k \le 5). The value p is the regular price per item (1p999)(1 \le p \le 999). Notice that all together at most 5×5=255\times 5=25 items can be in the basket. The b+2ndb+2^{nd} line contains the number ss of special offers (0s99)(0 \le s \le 99). Each of the next ss 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 (1n5)(1 \le n \le 5). The next n pairs of numbers (c,k)(c,k) indicate that kk items (1k5)(1 \le k \le 5) with product code cc (1c999)(1 \le c \le 999) are involved in the offer. The last number pp on the line stands for the reduced price (1p9999)(1 \le p \le 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

1
2
3
4
5
6
2
7 3 2
8 2 5
2
1 7 3 5
2 7 1 8 2 10

Sample Output

1
14

标签:五维DP

Translation

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

Solution

一道大水题。因为只有五种物品,可以用五维的dpdp来表示,五个维度分别表示五种物品的个数,即f[i][j][p][q][r]f[i][j][p][q][r]表示第一种物品取ii个,第二种物品取jj个…第五种物品取rr个至少需要多少钱。把每种物品的单价看作一种只有一个物品的套餐,这样枚举套餐,看能否转移。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cstdio>
#define MAX_N 5
#define MAX_S 100
#define MAX_C 1000
#define INF 2147483647
using namespace std;
int n, m, cnt, basket[MAX_N+5], offer[MAX_S+10][MAX_N+5], v[MAX_S+10], map[MAX_C+5];
int f[MAX_N+5][MAX_N+5][MAX_N+5][MAX_N+5][MAX_N+5];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int id, num, p;
scanf("%d%d%d", &id, &num, &p);
map[id] = i, basket[i] = num, offer[cnt][i] = 1, v[cnt++] = p;
}
scanf("%d", &m);
for (int i =0; i < m; i++) {
int num, id, k, p;
scanf("%d", &num);
for (int i = 0; i < num; i++) {
scanf("%d%d", &id, &k);
offer[cnt][map[id]] = k;
}
scanf("%d", &p);
v[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) {f[a0][a1][a2][a3][a4] = 0; continue;}
f[a0][a1][a2][a3][a4] = INF;
for (int i = 0; i < cnt; i++) {
if (a0 < offer[i][0] || a1 < offer[i][1] || a2 < offer[i][2] || a3 < offer[i][3] || a4 < offer[i][4]) continue;
f[a0][a1][a2][a3][a4] = min(f[a0][a1][a2][a3][a4], f[a0-offer[i][0]][a1-offer[i][1]][a2-offer[i][2]][a3-offer[i][3]][a4-offer[i][4]]+v[i]);
}
}
printf("%d", f[basket[0]][basket[1]][basket[2]][basket[3]][basket[4]]);
return 0;
}