HDU 1170 Shopping Offers 离散+状态压缩+完全背包
题目链接:
http://poj.org/problem?id=1170
Shopping Offers
Time Limit: 1000MSMemory Limit: 10000K
#### 问题描述
> In a shop each kind of product has a price. For example, the price of a flower is 2 ICU (Informatics Currency Units) and the price of a vase is 5 ICU. In order to attract more customers, the shop introduces some special offers.
> A special offer consists of one or more product items for a reduced price. Examples: three flowers for 5 ICU instead of 6, or two vases together with one flower for 10 ICU instead of 12.
> Write a program that calculates the price a customer has to pay for certain items, making optimal use of the special offers. That is, the price should be 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 14 ICU: two vases and one flower for the reduced price of 10 ICU and two flowers for the regular price of 4 ICU.
输入
Your program is to read from standard input. The first line contains the number b of different kinds of products in the basket (0 <= b <= 5). Each of the next 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 in the basket (1 <= k <= 5). The value p is the regular price per item (1 <= p <= 999). Notice that all together at most 5*5=25 items can be in the basket. The b+2nd line contains the number s of special offers (0 <= s <= 99). Each of the next s lines describes one offer by giving its structure and its reduced price. The first number n on such a line is the number of different kinds of products that are part of the offer (1 <= n <= 5). The next n pairs of numbers (c,k) indicate that k items (1 <= k <= 5) with product code c (1 <= c <= 999) are involved in 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.
输出
Your program is to write to standard output. Output one line with the lowest possible price to be paid.
样例输入
2
7 3 2
8 2 5
2
1 7 3 5
2 7 1 8 2 10
样例输出
14
题意
告诉你每类产品的单价,和打算购买的数量。现在有些优惠活动,买一些特定数量的商品组合能够优惠,问你如何用最少的钱买到需要购买的商品。
题解
dp[i][j][k][l][m]代表购买了i个商品0...m个商品4,需要花的最少的money,然后每个优惠活动和单价都可以看作转移边。前面那个状态可以用六进制压缩下状态。
这样转化成了一个完全背包的额问题了。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=7800;
struct In {
int id,c,v;
bool operator < (const In& tmp) const {
return id<tmp.id;
}
} in[11];
int dp[maxn],a[11],b[11],xp[11];
int main() {
xp[0]=1; for(int i=1;i<11;i++) xp[i]=xp[i-1]*6;
int n,m;
while(scf("%d",&n)==1) {
VI ha;
VPII arr;
for(int i=0; i<n; i++) {
scf("%d%d%d",&in[i].id,&in[i].c,&in[i].v);
ha.pb(in[i].id);
}
sort(all(ha));
sort(in,in+n);
int last=0;
for(int i=0;i<n;i++){
last+=xp[i]*in[i].c;
arr.pb(mkp(xp[i],in[i].v));
}
scf("%d",&m);
for(int i=0;i<m;i++){
int cnt; scf("%d",&cnt);
int stat=0;
while(cnt--){
int id,c;
scf("%d%d",&id,&c);
int p=lower_bound(all(ha),id)-ha.begin();
stat+=c*xp[p];
}
int v; scf("%d",&v);
arr.pb(mkp(stat,v));
}
clr(dp,-1);
dp[0]=0;
for(int i=1;i<=last;i++){
for(int j=0;j<arr.sz();j++){
int stat=arr[j].X,v=arr[j].Y;
if(i-stat>=0&&dp[i-stat]>=0){
if(dp[i]==-1) dp[i]=dp[i-stat]+v;
else dp[i]=min(dp[i],dp[i-stat]+v);
}
}
}
prf("%d\n",dp[last]);
}
return 0;
}
//end-----------------------------------------------------------------------
HDU 1170 Shopping Offers 离散+状态压缩+完全背包的更多相关文章
- POJ 1170 Shopping Offers非状态压缩做法
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5659 Accepted: 2361 Descr ...
- hdu 6125 -- Free from square(状态压缩+分组背包)
题目链接 Problem Description There is a set including all positive integers that are not more then n. Ha ...
- 背包系列练习及总结(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 ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- 【bzoj1688】[USACO2005 Open]Disease Manangement 疾病管理 状态压缩dp+背包dp
题目描述 Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Far ...
- HDU 3681 Prison Break(状态压缩dp + BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...
- HDU 1074 Doing Homework【状态压缩DP】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- hdu 4649 Professor Tian 反状态压缩+概率DP
思路:反状态压缩——把数据转换成20位的01来进行运算 因为只有20位,而且&,|,^都不会进位,那么一位一位地看,每一位不是0就是1,这样求出每一位是1的概率,再乘以该位的十进制数,累加,就 ...
随机推荐
- delphi7 TRichView 安装
下载: 链接: https://pan.baidu.com/s/1gfMYeGF 密码: 45bn 打开目录:E:\Delphi7\TRichView.v.16.10.3 ScaleRichView. ...
- 2017-2018-2 20155315《网络对抗技术》Exp1:PC平台逆向破解
实验目的 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...
- 《Java 程序设计》课堂实践项目-Arrays和String单元测试
<Java 程序设计>课堂实践项目-Arrays和String单元测试 课后学习总结 目录 改变 Arrays和String单元测试实验要求 课堂实践成果 课后思考 改变 修改了博客整体布 ...
- RabbitMQ(三):消息持久化策略
原文:RabbitMQ(三):消息持久化策略 一.前言 在正常的服务器运行过程中,时常会面临服务器宕机重启的情况,那么我们的消息此时会如何呢?很不幸的事情就是,我们的消息可能会消失,这肯定不是我们希望 ...
- c++ 文件位置相关操作
教学内容: l 文件定位操作 l fgetpos定位 l fsetpos设定位置 l 文件结束判断函数feof 一.文件定位操作 在C语言标准库里 获取文件位置的函数有ftell和fge ...
- 【mysql】排序方法
查询各科成绩前三名的记录,不考虑并列的情况: select a.course_id as 课程ID, a.score as 成绩, count(a.course_id) as 排名 from scor ...
- Spring MVC的Rest URL 被错误解析成jsp, 导致404错误(XML方式下@Controller和@RestController需要配置<mvc:annotation-driving/>)
问题: 最近在原有MVC的WEB应用中添加一个REST服务,结果始终报404错误.把 Spring debug 日志打开,发现处理REST请求的Controller已经正确进入 [org.spring ...
- dpkg使用记录
dpkg -l 查看所有已安装的包 grep即可过滤想要的内容 dpkg -r 包名 // 卸载包 -P 完全卸载 可能会有配置文件不能删除 不能删除的重启再卸载即可 dpkg -i 包 ...
- 负载均衡@StackExchange.Redis实现Session外置--纯干货喂饱你
Redis和StackExchange.Redis redis有多个数据库1.redis 中的每一个数据库,都由一个 redisDb 的结构存储.其中,redisDb.id 存储着 redis 数据库 ...
- 使用MUART0-P-1-2设置无线PM2.5感测环境
信息搜集–> 处理分析–> 动作执行,这是IoT环境中最基本的组成要素,传感器搜集环境信息后,透过指定的通讯协议传送到至控制中枢,经过处理分析后再将命令送交各device端执行.要实现这样 ...