USACO 3.3 Shopping Offers

Posted sigongzi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

 

以上是关于USACO 3.3 Shopping Offers的主要内容,如果未能解决你的问题,请参考以下文章

洛谷P2732 商店购物 Shopping Offers

Shopping Offers

[Leetcode] DP-- 638. Shopping Offers

POJ - 1170 Shopping Offers (五维DP)

[Leetcode]638. Shopping Offers

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