poj - 1170 - Shopping Offers(减少国家dp)
意甲冠军:b(0 <= b <= 5)商品的种类,每个人都有一个标签c(1 <= c <= 999),有需要购买若干k(1 <= k <=5),有一个单价p(1 <= p <= 999)。有s(0 <= s <= 99)。问完毕採购最少须要多少钱。
题目链接:http://poj.org/problem?id=1170
——>>已有b种物品,再将每种优惠分别看成一种新物品,剩下就是全然背包问题了。。
设dp[i]表示购买状态为 i 时的最少花费(关于购买状态:00032表示第0种物品买2个,第1种物品买3个)。则状态转移方程为:
dp[i + product[j].nState] = min(dp[i + product[j].nState], dp[i] + product[j].nPrice)(j是枚举各种物品的物品下标);
#include <cstdio>
#include <cstring>
#include <algorithm> using std::min; const int MAXN = 5 + 1;
const int MAXS = 6 * 6 * 6 * 6 * 6;
const int MAX_SIX = 6;
const int MAX_ID = 999 + 1;
const int MAX_OFFER = 99 + 1; struct PRODUCT
{
int nId;
int nNum;
int nPrice;
int nState;
} product[MAXN + MAX_OFFER]; int nType;
int dp[MAXS];
int nTargetState;
int nSixPow[MAX_SIX];
int nId2Bit[MAX_ID]; void Init()
{
nType = 0;
nTargetState = 0;
} void GetSixPow()
{
nSixPow[0] = 1;
for (int i = 1; i < MAX_SIX; ++i)
{
nSixPow[i] = nSixPow[i - 1] * 6;
}
} void CompletePack()
{
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
for (int i = 0; i < nTargetState; ++i)
{
for (int j = 0; j < nType; ++j)
{
if (i + product[j].nState > nTargetState) continue;
dp[i + product[j].nState] = min(dp[i + product[j].nState], dp[i] + product[j].nPrice);
}
}
printf("%d\n", dp[nTargetState]);
} int main()
{
int b, s, n; GetSixPow();
while (scanf("%d", &b) == 1)
{
Init();
for (int i = 0; i < b; ++i)
{
scanf("%d%d%d", &product[i].nId, &product[i].nNum, &product[i].nPrice);
product[i].nState = nSixPow[i];
nTargetState += nSixPow[i] * product[i].nNum;
nId2Bit[product[i].nId] = i;
}
scanf("%d", &s);
for (int i = 0; i < s; ++i)
{
int nId, nCnt;
product[b + i].nState = 0;
scanf("%d", &n);
for (int j = 0; j < n; ++j)
{
scanf("%d%d", &nId, &nCnt);
product[b + i].nState += nSixPow[nId2Bit[nId]] * nCnt;
}
scanf("%d", &product[b + i].nPrice);
}
nType = b + s;
CompletePack();
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
poj - 1170 - Shopping Offers(减少国家dp)的更多相关文章
- POJ - 1170 Shopping Offers (五维DP)
题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维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: 4696 Accepted: 1967 D ...
- POJ 1170 Shopping Offers非状态压缩做法
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5659 Accepted: 2361 Descr ...
- 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种套餐买法(套餐会便宜些),问最少要花多少钱. 思路:题目是完全背包,但这道题目不好处理的是套餐的状态, ...
- Chapter06-Phylogenetic Trees Inherited(POJ 2414)(减少国家DP)
Phylogenetic Trees Inherited Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 480 Accepted ...
- poj 1185 火炮 (减少国家DP)
火炮 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19690 Accepted: 7602 Description 司 ...
- HDU 1170 Shopping Offers 离散+状态压缩+完全背包
题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...
随机推荐
- fileziller 恢复 站点管理器 内的ftp帐号方法
由于系统坏了重装了系统,以前的fileziller中配置的服务器链接信息列表很多,新装fileziller后即使复制以前的安装目录过来,站点管理器内还是空荡荡的. 这些服务器链接的配置信息非常重要,如 ...
- 【图像处理】Gabor过滤器
Gabor内核参考wiki 使用实数Real的公式计算核函数代码: Mat getGaborFilter(float lambda, float theta, float sigma2,float g ...
- 重操JS旧业第八弹:面向对象与继承
js里面没有语言语法层面的继承机制,但这并不意味着js就不能实现继承,利用js属性和方法动态性来模拟实现继承,通过总结大概有如下方法实现: 1 原型链继承 我们知道原型在对象中扮演着重要的角色,函数本 ...
- vld(Visual Leak Detector) 内存泄露检测工具
初识Visual Leak Detector 灵活自由是C/C++语言的一大特色,而这也为C/C++程序员出了一个难题.当程序越来越复 杂时,内存的管理也会变得越加复杂,稍有不慎就会出现内存问题.内存 ...
- u盘安装ubuntu10.04 server.txt
10.04 先将 ubuntu server 的 iso 放到优盘上,然后在提示无法找到光驱时,按 alt+f2 打开一个新的 console 窗口,将 iso mount 上,具体操作如下: ls ...
- smartforms换页,
smartforms换页,可以把最后的窗口设置成最终窗口, 这样页码有可以用系统变量控制: 总页码/页码 :&SFSY-FORMPAGES&
- PreTranslateMessage和TranslateMessage区别
PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗口的消息都要通过这里,比较常用,当需要在MFC之前处理某些消息时,常常要在这里添加代码. ...
- cocos2d-x游戏开发系列教程-前言
cocos2d-x游戏开发前景: 最近企业对于Cocos2D-X开发人才的用人需求很大,而且所提供的薪资相当可观. 为满足广大向往游戏开发行业同学的需求,特推出适合新手的Cocos2D-X手游开发教程 ...
- c++在string类源
一:回想 (1)c++中的string类是在面试中和笔试中常常考的题目: project代码免费下载 string类的自行实现 (2)c++中的string类和fstream类合起来是处理外部数据的利 ...
- vs2005及以上版本的程序分发问题
我们使用vs2005及以上版本编译的应用程序(C/C++),在客户机器运行时,会出现: “由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这个问题” 那么,我们怎么解决这个问题呢 ...