usaco3.3.2 Shopping Offers

来源:互联网 发布:网络存储有什么用 编辑:程序博客网 时间:2024/04/28 23:02

一 原题

Shopping Offers
IOI'95

In a certain shop, each kind of product has an integer price. For example, the price of a flower is 2 zorkmids (z) and the price of a vase is 5z. In order to attract more customers, the shop introduces some special offers.

A special offer consists of one or more product items together for a reduced price, also an integer. Examples:

  • three flowers for 5z instead of 6z, or
  • two vases together with one flower for 10z instead of 12z.

Write a program that calculates the price a customer has to pay for a purchase, making optimal use of the special offers to make the price 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 14z: two vases and one flower for the reduced price of 10z and two flowers for the regular price of 4z.

PROGRAM NAME: shopping

INPUT FORMAT

The input file has a set of offers followed by a purchase.

Line 1:s, the number of special offers, (0 <= s <= 99).Line 2..s+1:Each line describes an offer using several integers. The first integer is n (1 <= n <= 5), the number of products that are offered. The subsequent n pairs of integers c and k indicate that k items (1 <= k <= 5) with product code c (1 <= c <= 999) are part of 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.Line s+2:The first line contains the number b (0 <= b <= 5) of different kinds of products to be purchased.Line s+3..s+b+2:Each of the subsequent 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 to be purchased (1 <= k <= 5). The value p is the regular price per item (1 <= p <= 999). At most 5*5=25 items can be in the basket.

SAMPLE INPUT (file shopping.in)

21 7 3 52 7 1 8 2 1027 3 28 2 5

OUTPUT FORMAT

A single line with one integer: the lowest possible price to be paid for the purchases.

SAMPLE OUTPUT (file shopping.out)

14


二 分析

DP。最多可以买5种商品,用dp[x1][x2][x3][x4][x5]代表5种物品分别买xi件的最低价格。少写一个头文件CE了一次。


三 代码

运行结果:
USER: Qi Shen [maxkibb3]TASK: shoppingLANG: C++Compiling...Compile: OKExecuting...   Test 1: TEST OK [0.000 secs, 4220 KB]   Test 2: TEST OK [0.000 secs, 4220 KB]   Test 3: TEST OK [0.000 secs, 4220 KB]   Test 4: TEST OK [0.000 secs, 4220 KB]   Test 5: TEST OK [0.011 secs, 4220 KB]   Test 6: TEST OK [0.000 secs, 4220 KB]   Test 7: TEST OK [0.000 secs, 4220 KB]   Test 8: TEST OK [0.000 secs, 4220 KB]   Test 9: TEST OK [0.000 secs, 4220 KB]   Test 10: TEST OK [0.054 secs, 4220 KB]   Test 11: TEST OK [0.097 secs, 4220 KB]   Test 12: TEST OK [0.076 secs, 4220 KB]All tests OK.

Your program ('shopping') produced all correct answers! This is yoursubmission #2 for this problem. Congratulations!


AC代码:
/*ID:maxkibb3LANG:C++PROB:shopping*/#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int MAX_ID = 1000;const int MAX_ITEM = 5;const int MAX_NUM = 6;const int MAX_PRICE = 1000;int S, N, C, K, P, B;int hashId[MAX_ID];int target[MAX_ITEM];int dp[MAX_NUM][MAX_NUM][MAX_NUM][MAX_NUM][MAX_NUM];struct Item {    int id;    int num;        int getHashId() {        return hashId[id];    }};Item item(const int &_id, const int &_num) {    Item ret;    ret.id = _id;    ret.num = _num;    return ret;}struct Discount {    vector<Item> items;    int price;        bool can(int i, int j, int k, int l, int m) {        for(int _i = 0; _i < items.size(); _i++) {            Item item = items[_i];            int id = item.getHashId();            if(id == -1) return false;            if(id == 0 && item.num > i) return false;            if(id == 1 && item.num > j) return false;            if(id == 2 && item.num > k) return false;            if(id == 3 && item.num > l) return false;            if(id == 4 && item.num > m) return false;        }        return true;    }    int get(int idx) {        for(int i = 0; i < items.size(); i++) {            if(items[i].getHashId() == idx)                 return items[i].num;        }        return 0;    }};vector<Discount> discounts;Discount discount(const vector<Item> &_items, const int &_price) {    Discount ret;    ret.items = _items;    ret.price = _price;    return ret;}void init() {    memset(hashId, -1, sizeof(hashId));    scanf("%d", &S);    for(int i = 0; i < S; i++) {        scanf("%d", &N);        vector<Item> items;        for(int j = 0; j < N; j++) {            scanf("%d%d", &C, &K);            items.push_back(item(C, K));        }        scanf("%d", &P);        discounts.push_back(discount(items, P));    }    scanf("%d", &B);    int cnt = 0;    for(int i = 0; i < B; i++) {        scanf("%d%d%d", &C, &K, &P);        vector<Item> items;        items.push_back(item(C, 1));        discounts.push_back(discount(items, P));        hashId[C] = cnt;        target[cnt] = K;        cnt++;    }        for(int i = 0; i <= target[0]; i++)    for(int j = 0; j <= target[1]; j++)    for(int k = 0; k <= target[2]; k++)    for(int l = 0; l <= target[3]; l++)    for(int m = 0; m <= target[4]; m++)        dp[i][j][k][l][m] = MAX_PRICE * MAX_ITEM * MAX_NUM;}void solve() {    dp[0][0][0][0][0] = 0;    for(int i = 0; i <= target[0]; i++)    for(int j = 0; j <= target[1]; j++)    for(int k = 0; k <= target[2]; k++)    for(int l = 0; l <= target[3]; l++)    for(int m = 0; m <= target[4]; m++) {        for(int n = 0; n < discounts.size(); n++) {            Discount d = discounts[n];            if(d.can(i, j, k, l, m)) {                dp[i][j][k][l][m] = min(dp[i][j][k][l][m],                        dp[i-d.get(0)][j-d.get(1)][k-d.get(2)][l-d.get(3)][m-d.get(4)] + d.price);            }        }    }    printf("%d\n", dp[target[0]][target[1]][target[2]][target[3]][target[4]]);}int main() {    freopen("shopping.in", "r", stdin);    freopen("shopping.out", "w", stdout);    init();    solve();    return 0;}



0 0