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 ...
随机推荐
- C++中extern关键字使用(转)
参考文章:http://blog.csdn.net/sruru/article/details/7951019 chapter1.如何混合编译C语言和C++ 实际开发过程中,C++中会调用C与语言编写 ...
- SGU 160.Magic Multiplying Machine
时间限制:0.5s 空间限制6M 题意: 给出n个(1<=n<=10000)1~m(2<m<1000)范围内的数,选择其中任意个数,使它们的 乘积 模m 最大,输 ...
- 【BZOJ1500】【块状链表】维修数列
Description Input 输入文件的第1行包含两个数N和M,N表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述 ...
- extern "C" {} 来沟通C和C++
比如说你用C++开发了一个DLL库,为了能够让C语言也能够调用你的DLL输出(Export)的函数,你需要用extern "C"来强制编译器不要修改你的函数名. 通常,在C语言的头 ...
- Java学习----集合框架总结
集合框架总结: Collection接口: Set接口: HashSet//对象必须实现hashCode方法,元素没有顺序呢,效率比LinkedHashSet高 LinkedHashSet//是Has ...
- CodeIgniter框架——介绍
CodeIgniter 是一个应用程序框架 CodeIgniter 是一个为用 PHP 编写网络应用程序的人员提供的工具包.它的目标是实现让你比从零开始编写代码更快速地开发项目,为此,CI 提供了一套 ...
- NPOI读写Excel0307
#region NPOI 操作 Excel 2007 /// <summary> /// 将Excel文件中的数据读出到DataTable中(xlsx) /// </summary& ...
- Wdcp两日志的路径
Wdcp两日志的路径: /www/wdlinux/httpd-2.2.22/logs /www/wdlinux/nginx-1.0.15/logs
- 最新发布C#.NET快速开发框架企业版V4.0 (适合开发ERP、进销存系统)
C/S系统开发框架-企业版 V4.0 (Enterprise Edition) http://www.csframework.com/cs-framework-4.0.htm 视频下载: 百度网盘: ...
- EasyUI篇のDataGrid
HTML: <table id="dg"></table> 或者 <div id="dg"></div> JS: ...