USACO 3.3 Shopping Offers

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)

2
1 7 3 5
2 7 1 8 2 10
2
7 3 2
8 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……但是我的暴搜没舍得扔,能过7个点正解:
  1 /*
  2 ID: ivorysi
  3 PROG: shopping
  4 LANG: C++
  5 */
  6 #include <iostream>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <set>
 12 #include <vector>
 13 #include <string.h>
 14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
 15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
 16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
 17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
 18 #define inf 0x3f3f3f3f
 19 #define MAXN 400005
 20 #define ivorysi
 21 #define mo 97797977
 22 #define ha 974711
 23 #define ba 47
 24 #define fi first
 25 #define se second
 26 #define pii pair<int,int>
 27 using namespace std;
 28 typedef long long ll;
 29 int id[1005],cnt;
 30 int s;
 31 int sale[7][7][7][7][7],item[7][7][7][7][7];
 32 int ti[6];
 33 int reg[7];
 34 void init() {
 35     scanf("%d",&s);
 36     int n,p,k[7],c,b,t;
 37     memset(sale,inf,sizeof(sale));
 38     siji(i,1,s) {
 39         scanf("%d",&n);
 40         memset(k,0,sizeof(k));
 41         siji(j,1,n) {
 42             scanf("%d%d",&c,&t);
 43             if(id[c]==0) id[c]=++cnt;
 44             k[id[c]]=t;
 45         }
 46         scanf("%d",&p);
 47         sale[k[1]][k[2]][k[3]][k[4]][k[5]]=min(p,sale[k[1]][k[2]][k[3]][k[4]][k[5]]);
 48     }
 49     scanf("%d",&b);
 50     siji(i,1,b) {
 51         scanf("%d%d%d",&c,&t,&p);
 52         if(id[c]==0) id[c]=++cnt;
 53         ti[id[c]]=t;
 54         reg[id[c]]=p;
 55     }
 56     siji(i,0,5) {
 57         siji(j,0,5){
 58             siji(m,0,5) {
 59                 siji(o,0,5) {
 60                     siji(l,0,5) {
 61                         item[i][j][m][o][l]=i*reg[1]+j*reg[2]+m*reg[3]+o*reg[4]+l*reg[5];
 62                     }
 63                 }
 64             }
 65         }
 66     }
 67 }
 68 int a1,a2,a3,a4,a5;
 69 void calc(int &x) {
 70     siji(i1,0,a1) {
 71         siji(i2,0,a2) {
 72             siji(i3,0,a3){
 73                 siji(i4,0,a4){
 74                     siji(i5,0,a5) {
 75                         int temp=sale[i1][i2][i3][i4][i5]+item[a1-i1][a2-i2][a3-i3][a4-i4][a5-i5];
 76                         x=min(x,temp);
 77                     }
 78                 }
 79             }
 80         }
 81     }
 82 }
 83 void solve() {
 84     init();
 85     for(a1=0;a1<=ti[1];++a1) {//a1,a2等都要重新赋为0
 86         for(a2=0;a2<=ti[2];++a2) {
 87             for(a3=0;a3<=ti[3];++a3) {
 88                 for(a4=0;a4<=ti[4];++a4){
 89                     for(a5=0;a5<=ti[5];++a5) {
 90                         calc(item[a1][a2][a3][a4][a5]);
 91                     }
 92                 }
 93             }
 94         }
 95     }
 96     printf("%d\n",item[ti[1]][ti[2]][ti[3]][ti[4]][ti[5]]);
 97 }
 98 int main(int argc, char const *argv[])
 99 {
100 #ifdef ivorysi
101     freopen("shopping.in","r",stdin);
102     freopen("shopping.out","w",stdout);
103 #else
104     freopen("f1.in","r",stdin);
105 #endif
106     solve();
107 }

暴搜:

  1 /*
  2 ID: ivorysi
  3 PROG: shopping
  4 LANG: C++
  5 */
  6 #include <iostream>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <set>
 12 #include <vector>
 13 #include <string.h>
 14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
 15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
 16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
 17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
 18 #define inf 0x7fffffff
 19 #define MAXN 400005
 20 #define ivorysi
 21 #define mo 97797977
 22 #define ha 974711
 23 #define ba 47
 24 #define fi first
 25 #define se second
 26 #define pii pair<int,int>
 27 using namespace std;
 28 typedef long long ll;
 29 int s;
 30 struct state {
 31     int val,item[1005];
 32     int& operator[](int x) {return item[x];}
 33     bool operator < (const state& rhs)const {
 34         bool flag=1;
 35         siji(i,1,999) {
 36             if(item[i]>rhs.item[i]) {flag=0;break;}
 37         }
 38         return flag;
 39     }
 40     bool operator ==(const state& rhs)const{
 41         return !memcmp(rhs.item,item,sizeof(item));
 42     }
 43     bool operator <=(const state& rhs)const{
 44         return (*this<rhs)||(*this==rhs);
 45     }
 46     state operator -(const state& rhs)const {
 47         state res;
 48         siji(i,1,999) {
 49             res[i]=item[i]-rhs.item[i];
 50         }
 51         return res;
 52     }
 53 }qwq,sale[105];
 54 int re[1005];
 55 int ans;
 56 void init() {
 57     scanf("%d",&s);
 58     int n,p,k,c,b;
 59     siji(i,1,s) {
 60         scanf("%d",&n);
 61         siji(j,1,n) {
 62             scanf("%d%d",&c,&k);
 63             sale[i][c]=k;
 64         }
 65         scanf("%d",&p);
 66         sale[i].val=p;
 67     }
 68     scanf("%d",&b);
 69     siji(i,1,b) {
 70         scanf("%d%d%d",&c,&k,&p);
 71         qwq[c]=k;qwq.val+=k*p;
 72         re[c]=p;
 73     }
 74     siji(i,1,s) {
 75         int z=0;
 76         siji(j,1,999) {
 77             z+=sale[i][j]*re[j];
 78         }
 79         sale[i].val-=z;
 80     }
 81     ans=qwq.val;
 82 }
 83 void dfs(state x) {
 84     if(x.val<ans) ans=x.val;
 85
 86     siji(i,1,s) {
 87         state nw=x;
 88         while(sale[i]<=x) {
 89             nw=nw-sale[i];
 90             nw.val+=sale[i].val;
 91             dfs(nw);
 92         }
 93     }
 94 }
 95 void solve() {
 96     init();
 97     dfs(qwq);
 98     printf("%d\n",ans);
 99 }
100 int main(int argc, char const *argv[])
101 {
102 #ifdef ivorysi
103     freopen("shopping.in","r",stdin);
104     freopen("shopping.out","w",stdout);
105 #else
106     freopen("f1.in","r",stdin);
107 #endif
108     solve();
109 }
 
时间: 2024-10-13 18:54:09

USACO 3.3 Shopping Offers的相关文章

洛谷P2732 商店购物 Shopping Offers

P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 在商店中,每一种商品都有一个价格(用整数表示).例如,一朵花的价格是 2 zorkmids (z),而一个花瓶的价格是 5z .为了吸引更多的顾客,商店举行了促销活动. 题目描述 促销活动把一个或多个商品组合起来降价销售,例如: 三朵花的价格是 5z 而不是 6z, 两个花瓶和一朵花的价格是 10z 而不是

Shopping Offers

Shopping Offers 在商店中,每一种商品都有一个价格(用整数表示).例如,一朵花的价格是 2 zorkmids (z),而一个花瓶的价格是 5z .为了吸引更多的顾客,商店举行了促销活动. 促销活动把一个或多个商品组合起来降价销售,例如: 三朵花的价格是 5z 而不是 6z, 两个花瓶和一朵花的价格是 10z 而不是 12z. 编写一个程序,计算顾客购买一定商品的花费,尽量利用优惠使花费最少.尽管有时候添加其他商品可以获得更少的花费,但是你不能这么做. 对于上面的商品信息,购买三朵花

背包系列练习( hdu 2844 Coins &amp;&amp; hdu 2159 &amp;&amp; poj 1170 Shopping Offers &amp;&amp; hdu 3092 Least common multiple &amp;&amp; poj 1015 Jury Compromise)

作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢http://blog.csdn.net/eagle_or_snail/article/details/50987044,这里有大部分比较有趣的dp练手题. hdu 2844 Coins 多重背包 就是一个10w的多重背包,每个物品的cost同时也作为value去做背包,我们求的是每个容量下的价值,所以没

【POJ 1170】 Shopping Offers [动态规划 状态压缩 背包][离散化]

POJ - 1170 Shopping Offers 放假打题 sufu 看完题我是懵比的 这....  emmmmm   瓜想了半个小时之后我选择狗带 然后点开链接 装压+dp!!!!哦!!!!!!巧妙!!!! 就先把目标状态还有各个优惠的状态处理好 然后就是一个完全背包处理用优惠 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int S=100+5,N=10,P=10000+5,C=1000+5; 4 int sale[S

usaco Shopping Offers(多重完全背包)

有个人要去超时买东西,超市推出了一些优惠信息,每个优惠信息可以使用多次,每条优惠信息限制了要使用这个优惠信息所需购买的物品种类和每种物品的数量. 求买下这个人想买得所有物品总共需要的最少花费. 分析之后以优惠券作为物品,以要买的物品总量为容量,花费的钱为价值. /* ID: modengd1 PROG: shopping LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #in

USACO Training Section 3.3 Shopping Offers

拿给出的每种方案作为一种物品其他的单卖的物品也作为一种物品 拿它们去跑背包就行 注意编号对应上就行 代码: #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cctype> #include <cstdio> #include <locale> #include <map> using

[Leetcode] DP-- 638. Shopping Offers

In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price. You are given the each item's price, a se

POJ - 1170 Shopping Offers (五维DP)

题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来表示.每一个维度都代表一件商品的数量 打折的方式事实上有b + s种.将每种商品单件卖的也算一种打折方式 这题有个坑点,就是b或者s有可能为0 #include<cstdio> #include<cstring> #include<algorithm> #include&l

poj - 1170 - Shopping Offers(状态压缩dp)

题意:b(0 <= b <= 5)种物品,每种有个标号c(1 <= c <= 999),有个需要购买的个数k(1 <= k <=5),有个单价p(1 <= p <= 999),有s(0 <= s <= 99)种组合优惠方案,问完成采购最少需要多少钱. 题目链接:http://poj.org/problem?id=1170 -->>已有b种物品,再将每种优惠分别看成一种新物品,剩下就是完全背包问题了.. 设dp[i]表示购买状态为 i