POJ - 1170 Shopping Offers (五维DP)
题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折。有s种打折方式。问怎么才干使买的价格达到最低
解题思路:最多仅仅有五种商品。且每件商品最多仅仅有5个,所以能够用5维dp来表示。每一个维度都代表一件商品的数量
打折的方式事实上有b + s种。将每种商品单件卖的也算一种打折方式
这题有个坑点,就是b或者s有可能为0
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 6
#define maxs 200
#define INF 0x3f3f3f3f
struct product{
int c, k, p;
}pro[maxn];
struct offer{
int num[maxn], price;
}off[maxs];
int b, s;
int dp[maxn][maxn][maxn][maxn][maxn];
map<int,int> m;
void init() {
memset(dp, -1, sizeof(dp));
memset(off, 0, sizeof(off));
m.clear();
for(int i = 0; i < b; i++) {
scanf("%d%d%d", &pro[i].c, &pro[i].k, &pro[i].p);
m[pro[i].c] = i;
off[i].num[i] = 1;
off[i].price = pro[i].p;
}
if(b < 5) {
for(int i = b; i < 5; i++)
pro[i].k = pro[i].p = 0;
}
scanf("%d", &s);
int n, c, k;
for(int i = b; i < b + s; i++) {
scanf("%d", &n);
for(int j = 0; j < n; j++) {
scanf("%d%d", &c, &k);
off[i].num[m[c]] += k;
}
scanf("%d", &off[i].price);
}
}
int dfs(int a1, int a2, int a3, int a4, int a5) {
if(dp[a1][a2][a3][a4][a5] != -1)
return dp[a1][a2][a3][a4][a5];
dp[a1][a2][a3][a4][a5] = INF;
for(int i = 0; i < b + s; i++) {
if(a1 >= off[i].num[0] && a2 >= off[i].num[1] && a3 >= off[i].num[2] && a4 >= off[i].num[3] && a5 >= off[i].num[4])
dp[a1][a2][a3][a4][a5] = min(dp[a1][a2][a3][a4][a5], off[i].price + dfs(a1-off[i].num[0],a2-off[i].num[1],a3-off[i].num[2],a4-off[i].num[3],a5-off[i].num[4]));
}
return dp[a1][a2][a3][a4][a5];
}
void solve() {
dp[0][0][0][0][0] = 0;
if(b == b + s) {
int t = 0;
for(int i = 0; i < 5; i++)
t += pro[i].p * pro[i].k;
printf("%d\n", t);
}
else
printf("%d\n",dfs(pro[0].k, pro[1].k, pro[2].k, pro[3].k, pro[4].k));
}
int main() {
scanf("%d", &b);
init();
solve();
return 0;
}
POJ - 1170 Shopping Offers (五维DP)的更多相关文章
- 背包系列练习及总结(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 ...
- POJ 1170 Shopping Offers非状态压缩做法
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5659 Accepted: 2361 Descr ...
- poj 1170 Shopping Offers
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4696 Accepted: 1967 D ...
- poj - 1170 - Shopping Offers(减少国家dp)
意甲冠军:b(0 <= b <= 5)商品的种类,每个人都有一个标签c(1 <= c <= 999),有需要购买若干k(1 <= k <=5),有一个单价p(1 & ...
- POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)
题目地址:http://poj.org/problem?id=1170 Description In a shop each kind of product has a price. For exam ...
- POJ 1170 Shopping Offers(完全背包+哈希)
http://poj.org/problem?id=1170 题意:有n种花的数量和价格,以及m种套餐买法(套餐会便宜些),问最少要花多少钱. 思路:题目是完全背包,但这道题目不好处理的是套餐的状态, ...
- HDU 1170 Shopping Offers 离散+状态压缩+完全背包
题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...
- POJ 1661 Help Jimmy(二维DP)
题目链接:http://poj.org/problem?id=1661 题目大意: 如图包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的 ...
- luogu 4401 矿工配餐 多维dp
五维dp,记忆化搜索会MLE超内存,所以用滚动数组,十分经典 五维dp #include <bits/stdc++.h> using namespace std; ; ][][][],la ...
随机推荐
- 我的css reset
@charset "utf-8"; /*reset*/ body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,f ...
- phpcms get标签说明
{pc:get sql="SELECT * FROM phpcms_member" cache="3600" page="$page" db ...
- Shell 控制并发
方法1: #!/bin/bash c=0 for i in `seq -w 18 31`;do while [ $c -ge 3 ];do c=$(jobs -p |wc -w) sleep 1s d ...
- 首页重定位到mian.action上
<body onload="top.location.href='<%=request.getContextPath()%>/main.action';">
- linux 消息队列的限制
消息队列的系统限制 作者:冯老师,华清远见嵌入式学院讲师. 消息队列是System V的IPC对象的一种,用于进程间通信,会受到系统的限制,本文主要描述了三个限制.第一:议个消息的最大长度:第二:消息 ...
- 使用next-key locks 用于搜索和索引扫描,可以防止幻读
Next-Key Locks A next-key lock is a combination of a record lock on the index record and a gap lock ...
- iostat 使用说明
LINUX [oracle@perass back]$ iostat -m 1 10 Linux 2.6.18-194.el5 (perass) 03/01/2014 avg-cpu: %user % ...
- UVA- 1504 - Genghis Khan the Conqueror(最小生成树-好题)
题意: n个点,m个边,然后给出m条边的顶点和权值,其次是q次替换,每次替换一条边,给出每次替换的边的顶点和权值,然后求出这次替换的最小生成树的值; 最后要你输出:q次替换的平均值.其中n<30 ...
- weblogic启动报错之未修改hosts产生错误
报错如下: Enter username to boot WebLogic server:weblogic Enter password to boot WebLogic server: <Ju ...
- 【模拟】Codeforces 691B s-palindrome
题目链接: http://codeforces.com/problemset/problem/691/B 题目大意: 求一个字符串是不是镜像的(不是回文).是输出TAK否则RE. 题目思路: [模拟] ...