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 ...
随机推荐
- Codeforces 543C Remembering Strings(DP)
题意比较麻烦 见题目链接 Solution: 非常值得注意的一点是题目给出的范围只有20,而众所周知字母表里有26个字母.于是显然对一个字母进行变换后是不影响到其它字符串的. 20的范围恰好又是常见状 ...
- POJ 1830.开关问题(高斯消元)
题目链接 Solutin: 将每个开关使用的情况当成未知数,如果开关i能影响到开关j,那么系数矩阵A[j][i]的系数为1. 每个开关增广矩阵的值是开关k的初状态异或开关k的目标状态,这个应该很容易想 ...
- 如何用angularjs制作一个完整的表格之五__完整的案例
由于本人也是边学边写,因此整理的比较乱,下面放出我例子的完整代码,方便大家交流测试,如有问题欢迎评论 首先,表格采用的是BootStrap样式编辑的,主要使用的是angularjs,为了方便也有jQu ...
- Wdcp两日志的路径
Wdcp两日志的路径: /www/wdlinux/httpd-2.2.22/logs /www/wdlinux/nginx-1.0.15/logs
- 安卓AVD使用建议
问题描述:之前在安装了Android开发环境后,一开始并没有直接在Android手机和平板上进行调试,是使用的AVD模拟器工具.由于电脑的配置不是特别好,总感觉AVD的使用速度太慢,包括启动的时候还有 ...
- linux下shapely的安装
错误 1.“from shapely.geometry import Point, LineString, Polygon”时报错: OSError: Could not find library g ...
- Insert Interval 面试题leetcode.
刚开始做这个题的时候绕了好大的圈,对问题的分析不全面,没能考虑所有情况,做的很纠结.后来看了下大神的做法很受启发,改了改代码,最终提交了. public static ArrayList<Int ...
- 常用 Linux 命令
Check page size: getconf PAGESIZE Check memory information: cat /proc/meminfo Check number of hugepa ...
- 1、MyBatisNet的安装使用
用到的几个DLL按理说应该到官网下载,但这个官网是谷大哥的,不知道是不是被屏蔽,总打不开,幸好从别人的程序里拷过来一份,直接放在自己的程序里就行! 程序结构如下: Providers.config,S ...
- WPF开发技术介绍
本月做了一个小讲座,主要是WPF的开发技术介绍,由于是上班时间,去听的人不多,但对于自己来说至少是又巩固了Winform的知识,抽时间写一篇文章,在此分享给大家,有什么宝贵建议大家也可以提给我,谢谢. ...