设为首页 加入收藏

TOP

poj - 1170 - Shopping Offers(状态压缩dp)
2015-07-20 17:35:03 来源: 作者: 【 】 浏览:1
Tags:poj 1170 Shopping Offers 状态 压缩

题意: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 
  
   
#include 
   
     #include 
    
      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 2736 Housewife Wind(树链剖.. 下一篇Codeforces 385 D Bear and Flood..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Java 并发工具类:提 (2025-12-25 20:25:44)
·Java面试技巧:如何 (2025-12-25 20:25:41)
·Java并发编程中的线 (2025-12-25 20:25:38)
·C 语言 - cppreferen (2025-12-25 19:50:27)
·《C 语言入门教程》 (2025-12-25 19:50:23)