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. asp.net打印网页后自动关闭网页【无需插件】

    项目遇需要网页加载自动打印网页后需要自动关闭该网页,但是百度了好久发现都是需要插件什么的 于是就自己摸索摸索,用js弄了个定时器,意外的发现,当打印设置窗口弹出后,定时器就暂停了 不管你点击取消或者打 ...

  2. Linux网络编程(三)

    Linux网络编程(三) wait()还是waitpid() Linux网络编程(二)存在客户端断开连接后,服务器端存在大量僵尸进程.这是由于服务器子进程终止后,发送SIGCHLD信号给父进程,而父进 ...

  3. JS事件冒泡、停止冒泡、addEventListener--实例演示

    问题: <div class='item' id='outer' onclick="alert('outer')"> <div class='item' id=' ...

  4. javascript拾遗

    javascript中,只有null和undefined不能拥有方法,其他任何类型都可以在其上定义方法:字符串既然不是对象,怎么会有属性呢?只有引用了字符串的属性,那么javascript就会将字符串 ...

  5. C/C++单链表

    C/C++单链表 先看例子,例1:定义链表 //定义链表 struct stu { int name; int age; struct stu *next; }; 用一组地址任意的存储单元存放线性表中 ...

  6. 权限系统设计实现MVC4 + WebAPI + EasyUI + Knouckout

    权限系统设计实现MVC4 + WebAPI + EasyUI + Knouckout (一) 一.前言 之前的博客一直都还没写到框架的实现及权限系统,今天开始写我的权限系统,我以前做过的项目基本上都有 ...

  7. 关于ActiveMQ的一点总结

    ActiveMQ入门 作者:一路向北 摘要:本文主要讲述ActiveMQ的基本知识和使用方法,并简单结合spring使用ActiveMQ. 一.ActiveMQ特性和使用总览 企业消息软件从80年代起 ...

  8. c/c++性能优化--I/O优化(上)

    这节本想直接介绍I/O优化的,后来思考一下有必要对常用的I/O操作函数的特点介绍一下,这样要好些.下面就先介绍和I/O有关的库函数(以C99为准) 不同的操作系统有不同的文件管理方式,现行的主要有FA ...

  9. java对数据库的操作

    package com.DateSystem; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLEx ...

  10. 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载

    在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符. []运算符重载 +运算符重载 +=运算符重载 <<运算符重载 >>运算符重 ...