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. C++中文件的操作

    #include <iostream> #include <fstream> using namespace std; int main() { char s[27],m[27 ...

  2. BDD

    Binding business requirements to .NET code http://www.specflow.org/ 行为驱动开发 BDD:Behavior Driven Devel ...

  3. Orchard学习计划

    http://esshs.cnblogs.com/ http://orchardproject.net/http://www.orchardch.com/Documenthttps://orchard ...

  4. DOS头分析

    DOS头分析 PE文件结构综览: 首先上图片: 看到上面的图片可以清晰的看到PE结构复杂结构式什么样子的.有DOS首部,PE头部,PE节表,很多的表块,最后就是一些调试信息. DOS头由DOS 'MZ ...

  5. 学习Linux(一)环境搭建

    零基础学习Linux(一)环境搭建 从本文开始我会为大家介绍一下linux环境下详细的集群环境安装.配置.部署到实例演示的整个过程.在此过程中会给大家详细介绍一下Linux的操作技巧和一些工具的使用. ...

  6. TortoiseSVN使用方法 安装和配置

    TortoiseSVN使用方法   安装和配置 TortoiseSVN的下载地址为 http://tortoisesvn.net/downloads.html 有32位和64位的版本,一定要根据自己的 ...

  7. 学习SQL关联查询

    通过一个小问题来学习SQL关联查询 原话题: 是关于一个left join的,没有技术难度,但不想清楚不一定能回答出正确答案来: TabA表有三个字段Id,Col1,Col2 且里面有一条数据1,1, ...

  8. window.open()详解及浏览器兼容性问题

    一.基本语法:window.open(pageURL,name,parameters)其中:pageURL 为子窗口路径name  为子窗口名字parameters 为窗口参数(各参数用逗号分隔) 二 ...

  9. tomcat配置数据池

    1->配置servlet.xml 在 <GlobalNamingResources></GlobalNamingResources>中添加<Resource> ...

  10. TOGAF架构开发方法(ADM)之架构变更管理阶段

    TOGAF架构开发方法(ADM)之架构变更管理阶段 1.10 架构变更管理(Architecture Change Management) 企业架构开发方法各阶段——架构变更管理 1.10.1 目标 ...