当前位置: 首页 > news >正文

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;
}

相关文章:

  • IE兼容问题,各类css hack代码(亲测有效)
  • 一分钟了解阿里云产品:云数据库MongoDB版
  • eclipse部署Tomcat6 : The server does not support version 3.0 of the JEE Web module specification
  • 一行代码判断是否移动端
  • 结构型模式--装饰模式
  • launch文件概述---1
  • Python 修行摘要二
  • 安卓学习UI组件-Menu-选项菜单-上下文菜单-弹出式菜单
  • PHP学习笔记(3)-Zend Studio安装和汉化
  • mode(思维,注意内存)
  • 给新手的新浪微博 SDK 集成教程【一】
  • GDC2016【全境封锁(Tom Clancy's The Division)】对为何对应Eye Tracked System,以及各种优点的演讲报告...
  • [na]wac无线控制器集中转发部署的几种情况
  • C语言中内存分配问题
  • Nyoj 吝啬的国度(图论双DFS)
  • Docker下部署自己的LNMP工作环境
  • ECMAScript6(0):ES6简明参考手册
  • git 常用命令
  • HTTP那些事
  • java多线程
  • leetcode98. Validate Binary Search Tree
  • PaddlePaddle-GitHub的正确打开姿势
  • php ci框架整合银盛支付
  • SpringBoot 实战 (三) | 配置文件详解
  • tensorflow学习笔记3——MNIST应用篇
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 关于Flux,Vuex,Redux的思考
  • 后端_MYSQL
  • 技术发展面试
  • 面试遇到的一些题
  • 爬虫模拟登陆 SegmentFault
  • 世界上最简单的无等待算法(getAndIncrement)
  • 通信类
  • 《码出高效》学习笔记与书中错误记录
  • 昨天1024程序员节,我故意写了个死循环~
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #调用传感器数据_Flink使用函数之监控传感器温度上升提醒
  • (+4)2.2UML建模图
  • (2)STL算法之元素计数
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (solr系列:一)使用tomcat部署solr服务
  • (附源码)spring boot儿童教育管理系统 毕业设计 281442
  • (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
  • *p++,*(p++),*++p,(*p)++区别?
  • .NET Core 和 .NET Framework 中的 MEF2
  • .Net Memory Profiler的使用举例
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • @hook扩展分析
  • []Telit UC864E 拨号上网
  • [Angular] 笔记 18:Angular Router
  • [BZOJ 4129]Haruna’s Breakfast(树上带修改莫队)
  • [BZOJ2281][SDOI2011]黑白棋(K-Nim博弈)