意甲冠军: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)的更多相关文章

  1. POJ - 1170 Shopping Offers (五维DP)

    题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来 ...

  2. 背包系列练习及总结(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 ...

  3. poj 1170 Shopping Offers

    Shopping Offers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4696   Accepted: 1967 D ...

  4. POJ 1170 Shopping Offers非状态压缩做法

    Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5659 Accepted: 2361 Descr ...

  5. POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)

    题目地址:http://poj.org/problem?id=1170 Description In a shop each kind of product has a price. For exam ...

  6. POJ 1170 Shopping Offers(完全背包+哈希)

    http://poj.org/problem?id=1170 题意:有n种花的数量和价格,以及m种套餐买法(套餐会便宜些),问最少要花多少钱. 思路:题目是完全背包,但这道题目不好处理的是套餐的状态, ...

  7. Chapter06-Phylogenetic Trees Inherited(POJ 2414)(减少国家DP)

    Phylogenetic Trees Inherited Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 480 Accepted ...

  8. poj 1185 火炮 (减少国家DP)

    火炮 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19690   Accepted: 7602 Description 司 ...

  9. HDU 1170 Shopping Offers 离散+状态压缩+完全背包

    题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...

随机推荐

  1. java设计模式之观察者模式(9)

    Java观察者模式的浅析 简单地说,观察者模式定义了一个一对多的依赖关系,让一个或多个观察者对象监察一个主题对象.这样一个主题对象在状态上的变化能够通知所有的依赖于此对象的那些观察者对象,使这些观察者 ...

  2. [Cocos2d-x开发问题-3] cocos2dx动画Animation介绍

    Cocos2d-x为了减少开发难度,对于动画的实现採用的帧动画的方案.这也就是说Cocos2d-x中的动画是帧动画. 帧动画的原理相信大家都不陌生,就是多张图片循环播放以实现动画的效果. 一个简单的动 ...

  3. MSSQL - 存储过程事物

    效果: 创建带有事物的存储过程: use sales --指定数据库 create table bb --创建bb 这个表 ( ID int not null primary key ,--账号 Mo ...

  4. GreenDAO数据库版本升级

    GreenDAO是一款非要流行的android平台上的数据库框架,性能优秀,代码简洁. 初始化数据库模型代码的时候需要使用java项目生成代码,依赖的jar包已经上传到我的资源里了,下载地址如下:ht ...

  5. File Templates for web.xml & web-fragment.xml (Servlet 2.3, 2.4, 2.5 + 3.0)

    As I sometimes need these, I have compiled a list of the valid headers of the web.xml and web-fragme ...

  6. JavaScript编程:文档对象模型DOM

    5.文档对象模型DOM JS里的DOM和XML里的DOM不同,但是解析方式是一样的. document.getElementByID("id")根据ID获得元素节点. 创建和操作节 ...

  7. EasyUI - Tooltip 提示控件

    第一种: 效果: html代码: 不需要js代码,显示的是title中的内容. <div> <a href=</a> </div> 第二种: 效果: html ...

  8. JDBC操作数据库的学习(1)

    单单对数据库的操作,比如说MySQL,我们可以在命令行窗口中执行,但是一般是应用程序要操作数据库,因此我们应该在程序中的代码上体现对数据库的操作,那么使用程序应用如何操作数据库呢?那就要使用到数据库的 ...

  9. 14.2.1 MySQL and the ACID Model

    14.2 InnoDB Concepts and Architecture InnoDB 概念和结构体系: 14.2.1 MySQL and the ACID Model 14.2.2 InnoDB ...

  10. clear_logs.py修改

    #!/usr/bin/env python#encoding=utf-8"""清空最后修改时间跑今天7天以前的所有文件"""#指定监控的路径 ...