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 ...
随机推荐
- android——使用pull解析xml文件
1.persons.xml 将persons.xml文件放到src目录下.其代码如下: <?xml version='1.0' encoding='UTF-8' standalone='yes' ...
- windows 2003 域控制器(AD)的常规命令行操作以及修复
查询服务器的角色 Netdom query fsmo 强制升级操作主机角色(如果两台DC都无损,可以直接用图形模式传送,这里指的是一台DC出问题,另一台强制升级占用角色的情况) Ntdsutil Ro ...
- android launcher开发之图标背景以及默认配置
1:然后我自己看了一下桌面图标的载入过程: 桌面第一次载入时是默认读取一个xml配置文件,完毕配置工作.这个配置文件在Launcher文件夹下, 路径是:\Launcher\res\xml\defau ...
- Arduino Nano + WIZ550io = 简易上网
我爱Arduino Nano – 这是一个非常好外形小巧却功能齐全的Arduino Uno.然而.当我去将它连接到互联网,全部的干净利落小巧也消失在大尺寸的以太网盾底下了. 只是,我近期发现了一个更好 ...
- 点击轮播图片左右button,实现轮播效果
点击左右button.实现图片轮播效果.js代码例如以下: $(function () { var index = 1; var pPage = 1; var $v_citemss = $(" ...
- redis+tomcat共享session问题(转)
为了让楼主看看Java开发是多么的简单和轻松,你说你耗时一周都没搞好,简直就是胡说八道!!我从没搞过reids和tomcat整合的(我做的项目一直都是去session用cookie,为了验证你在胡说八 ...
- 转载【浅谈ThreadPool 线程池】
浅谈ThreadPool 线程池 http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html
- Lucene.Net 2.3.1开发介绍 —— 三、索引(六)
原文:Lucene.Net 2.3.1开发介绍 -- 三.索引(六) 2.2 Field的Boost 如果说Document的Boost是一条线,那么Field的Boost则是一个点.怎么理解这个点呢 ...
- 与众不同 windows phone (3) - Application Bar(应用程序栏)
原文:与众不同 windows phone (3) - Application Bar(应用程序栏) [索引页][源码下载] 与众不同 windows phone (3) - Application ...
- 可能性dp+减少国家HDU4336
Card Collector Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...