传送门

Luogu团队题链接

解题思路

看到 \(m\) 这么小,马上想到状压 \(\text{DP}\)。

设 \(dp[i][j]\) 表示在前 \(i\) 家商店中已买商品的状态为 \(j\) 的最小花费。

但是有一点小问题,因为在同一家商店买多次物品时,只需要花一次路费,如果总是特判的话,就会比较麻烦,所以我们不妨先把路费全部加上,然后每次枚举一个物品向后转移,最后再把得到的结果和之前不加路费的去取 \(\min\)。

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} const int _ = 100 + 10;
const int __ = 16 + 2; int n, m, d[_], c[_][__], dp[_][1 << __]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m);
for (rg int i = 1; i <= n; ++i) {
read(d[i]);
for (rg int j = 1; j <= m; ++j) read(c[i][j]);
}
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
for (rg int i = 1; i <= n; ++i) {
for (rg int j = 0; j < 1 << m; ++j)
dp[i][j] = min(dp[i][j], dp[i - 1][j] + d[i]);
for (rg int j = 0; j < 1 << m; ++j)
for (rg int k = 1; k <= m; ++k)
if ((j >> (k - 1) & 1) ^ 1)
dp[i][j | 1 << (k - 1)] = min(dp[i][j | 1 << (k - 1)], dp[i][j] + c[i][k]);
for (rg int j = 0; j < 1 << m; ++j)
dp[i][j] = min(dp[i][j], dp[i - 1][j]);
}
printf("%d\n", dp[n][(1 << m) - 1]);
return 0;
}

完结撒花 \(qwq\)

「AMPPZ2014」The Prices的更多相关文章

  1. 题解【BZOJ4145】「AMPPZ2014」The Prices

    题目描述 你要购买 \(m\) 种物品各一件,一共有 \(n\) 家商店,你到第 \(i\) 家商店的路费为 \(d[i]\),在第 \(i\) 家商店购买第 \(j\) 种物品的费用为 \(c[i] ...

  2. [题解] [BZOJ4144] 「AMPPZ2014」Petrol

    题面 怎么是权限题啊 题解 有一次考过, 但是不记得了 如果每个点都是加油站的话, 这道题就是货车运输 考虑如何转化 我们可以设

  3. 「AMPPZ2014」The Captain

    传送门: 这是一道bzoj权限题 Luogu团队题链接 解题思路 直接连边的话边数肯定会爆炸,考虑减少边数. 我们画出坐标系,发现一个东西: 对于两个点 \(A,B\),\(|x_A-y_A|\) 可 ...

  4. 「AMPPZ2014」Petrol

    传送门: 这是一道bzoj权限题 Luogu团队题链接 解题思路 首先对于每一个点 \(x\) 预处理出 \(nr[x]\) 和 \(dis[x]\),分别表示离 \(x\) 最近的加油站以及该段距离 ...

  5. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  6. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  7. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  8. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  9. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

随机推荐

  1. SpringBoot+MyBatis+PageHelper分页无效

    POM.XML中的配置如下:<!-- 分页插件 --><!-- https://mvnrepository.com/artifact/com.github.pagehelper/pa ...

  2. JAVA分级测试——选课系统(补发)

    博客园似乎上传图片多了之后会出现各种问题,所以只能直接上代码了 <!DOCTYPE HTML> <html lang="zh"> <head> ...

  3. java怎么调用子类中父类被覆盖的方法

    public class b { { void show() { System.out.println("b"); } } public class c extends b { v ...

  4. (转)__attribute__之section 分析详解

    原文地址:__attribute__之section详解 前言 第一次接触 "section" 是在公司的一个STM32的项目代码中,前工程师将所有的初始化函数都使用的" ...

  5. Linux 上安装 Mysql 设置root密码问题

    Ubuntu 18.10.1 Mysql 5.7.26-0 安装mysql apt-get install mysql-server 安装完可以直接使用,但是新版本在安装过程中没有提示设置root用户 ...

  6. python笔记14

    今日内容 带参数的装饰器: flask框架 + django缓存 + 写装饰器实现被装饰的函数要执行N次 模块 os sys time(三种类型) datetime 和 timezone[了解] 内容 ...

  7. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)E(多重集维护)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long ans[1000007]; ...

  8. spm_hrf

    a=spm_hrf(0.72); n1=MOTOR_taskdesign(1,:);cn1=conv(n1,a);plot(cn1); block design hrf

  9. 一个元素的宽度 及带padding,border,margin的各自情况

    width() - 设置或返回元素的宽度 height() - 设置或返回元素的高度 innerWidth() - 返回元素的宽度(包含 padding) innerHeight() - 返回元素的高 ...

  10. LeetCode 101.对称二叉树 - JavaScript

    题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / \ 2 2 / \ / \ 3 4 4 3 解法 1:递归检查 根据题目" ...