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个点
正解:
 /*
ID: ivorysi
PROG: shopping
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
#define MAXN 400005
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
int id[],cnt;
int s;
int sale[][][][][],item[][][][][];
int ti[];
int reg[];
void init() {
scanf("%d",&s);
int n,p,k[],c,b,t;
memset(sale,inf,sizeof(sale));
siji(i,,s) {
scanf("%d",&n);
memset(k,,sizeof(k));
siji(j,,n) {
scanf("%d%d",&c,&t);
if(id[c]==) id[c]=++cnt;
k[id[c]]=t;
}
scanf("%d",&p);
sale[k[]][k[]][k[]][k[]][k[]]=min(p,sale[k[]][k[]][k[]][k[]][k[]]);
}
scanf("%d",&b);
siji(i,,b) {
scanf("%d%d%d",&c,&t,&p);
if(id[c]==) id[c]=++cnt;
ti[id[c]]=t;
reg[id[c]]=p;
}
siji(i,,) {
siji(j,,){
siji(m,,) {
siji(o,,) {
siji(l,,) {
item[i][j][m][o][l]=i*reg[]+j*reg[]+m*reg[]+o*reg[]+l*reg[];
}
}
}
}
}
}
int a1,a2,a3,a4,a5;
void calc(int &x) {
siji(i1,,a1) {
siji(i2,,a2) {
siji(i3,,a3){
siji(i4,,a4){
siji(i5,,a5) {
int temp=sale[i1][i2][i3][i4][i5]+item[a1-i1][a2-i2][a3-i3][a4-i4][a5-i5];
x=min(x,temp);
}
}
}
}
}
}
void solve() {
init();
for(a1=;a1<=ti[];++a1) {//a1,a2等都要重新赋为0
for(a2=;a2<=ti[];++a2) {
for(a3=;a3<=ti[];++a3) {
for(a4=;a4<=ti[];++a4){
for(a5=;a5<=ti[];++a5) {
calc(item[a1][a2][a3][a4][a5]);
}
}
}
}
}
printf("%d\n",item[ti[]][ti[]][ti[]][ti[]][ti[]]);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("shopping.in","r",stdin);
freopen("shopping.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}

暴搜:

 /*
ID: ivorysi
PROG: shopping
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define MAXN 400005
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
int s;
struct state {
int val,item[];
int& operator[](int x) {return item[x];}
bool operator < (const state& rhs)const {
bool flag=;
siji(i,,) {
if(item[i]>rhs.item[i]) {flag=;break;}
}
return flag;
}
bool operator ==(const state& rhs)const{
return !memcmp(rhs.item,item,sizeof(item));
}
bool operator <=(const state& rhs)const{
return (*this<rhs)||(*this==rhs);
}
state operator -(const state& rhs)const {
state res;
siji(i,,) {
res[i]=item[i]-rhs.item[i];
}
return res;
}
}qwq,sale[];
int re[];
int ans;
void init() {
scanf("%d",&s);
int n,p,k,c,b;
siji(i,,s) {
scanf("%d",&n);
siji(j,,n) {
scanf("%d%d",&c,&k);
sale[i][c]=k;
}
scanf("%d",&p);
sale[i].val=p;
}
scanf("%d",&b);
siji(i,,b) {
scanf("%d%d%d",&c,&k,&p);
qwq[c]=k;qwq.val+=k*p;
re[c]=p;
}
siji(i,,s) {
int z=;
siji(j,,) {
z+=sale[i][j]*re[j];
}
sale[i].val-=z;
}
ans=qwq.val;
}
void dfs(state x) {
if(x.val<ans) ans=x.val; siji(i,,s) {
state nw=x;
while(sale[i]<=x) {
nw=nw-sale[i];
nw.val+=sale[i].val;
dfs(nw);
}
}
}
void solve() {
init();
dfs(qwq);
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("shopping.in","r",stdin);
freopen("shopping.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}
 

USACO 3.3 Shopping Offers的更多相关文章

  1. 洛谷P2732 商店购物 Shopping Offers

    P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 在商店中, ...

  2. poj 1170 Shopping Offers

    Shopping Offers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4696   Accepted: 1967 D ...

  3. LeetCode 638 Shopping Offers

    题目链接: LeetCode 638 Shopping Offers 题解 dynamic programing 需要用到进制转换来表示状态,或者可以直接用一个vector来保存状态. 代码 1.未优 ...

  4. HDU 1170 Shopping Offers 离散+状态压缩+完全背包

    题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...

  5. 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)

    作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...

  6. Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers)

    Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers) 深度优先搜索的解题详细介绍,点击 在LeetCode商店中, 有许多在售的物品. 然而,也有一些大 ...

  7. LC 638. Shopping Offers

    In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...

  8. Week 9 - 638.Shopping Offers - Medium

    638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...

  9. POJ 1170 Shopping Offers非状态压缩做法

    Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5659 Accepted: 2361 Descr ...

随机推荐

  1. UVA 141 The Spot Game 斑点游戏。。

     The Spot Game  The game of Spot is played on an NxN board as shown below for N = 4. During the game ...

  2. mark_May

    嗯神忙的五月总算是过完了. 草草的做完研究性学习,浑浑噩噩的考了数学联赛,以及在考试的上一周还在疯狂的看未闻花名,貌似还有前几个星期不懂是吃错药还是怎样 总急着把2013的题目刷完=-=可是貌似到现在 ...

  3. [转]Creating an iPhone Daemon

    ref: http://chrisalvares.com/blog/7/creating-an-iphone-daemon-part-1/ http://chrisalvares.com/blog/3 ...

  4. 使用 ASP.NET MVC 4, EF, Knockoutjs and Bootstrap 设计和开发站点 - 6 - 业务逻辑

    翻译:使用 ASP.NET MVC 4, EF, Knockoutjs and Bootstrap 设计和开发站点 - 6 - 业务逻辑 Part 3: 设计逻辑层:核心开发 如前所述,我们的解决方案 ...

  5. ASP.NET Web API 2.0新特性:Attribute Routing1

    ASP.NET Web API 2.0新特性:Attribute Routing[上篇] 对于一个针对ASP.NET Web API的调用请求来说,请求的URL和对应的HTTP方法的组合最终决定了目标 ...

  6. 自用类库整理之SqlHelper和MySqlHelper

    自用类库整理之SqlHelper和MySqlHelper 自用的SQLHelper和MySqlHelper,除一些通用方法外,封装了一些很实用的批量操作方法,简单介绍下 SqlHelper Execu ...

  7. js的onclick和jquery的bind事件执行先后顺序

    近期在项目中为每一个ajax触发按钮写正在加载的效果,用的是bootstarp 代码如下 $(function(){ $('.btn').bind('click',function(e){ var $ ...

  8. 熬之滴水成石:Spring--精简的J2EE(7)

                                              49--持久性 关于持久性在最早的Delphi单元中,就曾介绍过.持久性是数据库中读取,保存,或删除数据的过程.毫无 ...

  9. iOS抽奖程序

    iOS抽奖程序 代码下载地址: http://vdisk.weibo.com/s/HKehU http://pan.baidu.com/share/link?shareid=893330225& ...

  10. 高反差保留滤镜学习OpenCV:滤镜系列(11)——高反差保留

    这几周笔者几篇文章介绍了改高反差保留滤镜的文章. 关联文章的地址 高反差保留就是高通滤波 r=(pix[x,y]-avg(R))/128 pix[x,y]*r+128*(1-r) #include & ...