bzoj4145 [AMPPZ2014]The Prices 状压 DP
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=4145
题解
好像这道题有不少方法呢。
...谁叫这道题有点简单,所以方法多呗。
我的方法:
求出 \(f[S]\) 表示要在同一家商店购买 \(S\) 中的物品的最小代价。
然后 \(dp[S]\) 表示购买 \(S\) 中的商品的最小代价。枚举子集转移即可。
时间复杂度 \(O(m2^n+3^n)\)。
还有一个不错的做法:
\(dp[i][S]\) 表示在前 \(i\) 个商店买 \(S\) 中的物品的最小代价。
于是转移的时候,如果决定要在 \(i\) 中买——那么先对所有状态加上路费,然后枚举每一个商品背包转移就可以了。
只写了我的做法。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
#define lowbit(x) ((x) & -(x))
const int N = 16 + 7;
const int M = 100 + 7;
const int NP = (1 << 16) + 7;
int m, n, S;
int a[M][N];
int f[NP], t[NP], dp[NP];
inline void ycl() {
S = (1 << n) - 1;
memset(f, 0x3f, sizeof(f));
for (int i = 1; i <= m; ++i) {
t[0] = a[i][0];
for (int s = 1; s <= S; ++s) t[s] = t[s ^ lowbit(s)] + a[i][std::__lg(lowbit(s)) + 1];
for (int s = 0; s <= S; ++s) smin(f[s], t[s]);
}
}
inline void work() {
ycl();
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
for (int s = 1; s <= S; ++s)
for (int sta = s; sta; sta = (sta - 1) & s)
smin(dp[s], f[sta] + dp[s ^ sta]);
printf("%d\n", dp[S]);
}
inline void init() {
read(m), read(n);
for (int i = 1; i <= m; ++i)
for (int j = 0; j <= n; ++j) read(a[i][j]);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
bzoj4145 [AMPPZ2014]The Prices 状压 DP的更多相关文章
- bzoj4145 AMPPZ2014 The Prices 状压dp
这个题.......很可以,很小清晰......反正正经的东西我都没想到:重点在于——————我不会处理那个多出来的路费所以当时我就骚骚的弄了一颗树包状压其实这是一个类01背包的状压在每个状态用01背 ...
- BZOJ.4145.[AMPPZ2014]The Prices(状压DP)
BZOJ 比较裸的状压DP. 刚开始写麻烦惹... \(f[i][s]\)表示考虑了前\(i\)家商店,所买物品状态为\(s\)的最小花费. 可以写求一遍一定去\(i\)商店的\(f[i]\)(\(f ...
- 【BZOJ4145】[AMPPZ2014]The Prices 状压DP
[BZOJ4145][AMPPZ2014]The Prices Description 你要购买m种物品各一件,一共有n家商店,你到第i家商店的路费为d[i],在第i家商店购买第j种物品的费用为c[i ...
- BZOJ 4145: [AMPPZ2014]The Prices( 状压dp + 01背包 )
我自己只能想出O( n*3^m )的做法....肯定会T O( nm*2^m )做法: dp( x, s ) 表示考虑了前 x 个商店, 已买的东西的集合为s. 考虑转移 : 先假设我们到第x个商店去 ...
- bzoj4145 [AMPPZ2014]The Prices(状压dp)
Description 你要购买m种物品各一件,一共有n家商店,你到第i家商店的路费为d[i],在第i家商店购买第j种物品的费用为c[i][j], 求最小总费用. Input 第一行包含两个正整数n, ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
随机推荐
- centos7中yum安装lamp环境
一.准备工作 1.1 环境 操作系统:centos7(CentOS-7-x86_64-Minimal-1708) 硬件:(这个根据项目运行和配置建议设置,一般我先配个1核1G) 1.2 关闭selin ...
- Windows建立目录软连接
创建:mklink /j "I:\dst" "I:\src" 删除: rmdir "I:\dst"
- redis centos集群搭建和java应用
1. 首先要ssh免密登录 redis集群,3台虚拟机,6个节点,每台机器2个节点一主一从. 192.168.132.154 c0192.168.132.156 c1192.168.132.155 c ...
- centos 7 环境准备工作
删除自带jdk: rpm -e --nodeps `rpm -qa | grep java` 打开 /etc/ 目录下的 profile 文件: vi /etc/profile 将如下代码追加到 pr ...
- EF框架之——Code First以及踩过的这些“坑”
传送门 Code First使用步骤 Code First报错和解决办法 以前在上海做了一段时间的Asp.net,基本用的都是.net自带的EF框架连接数据库,不过都是用的Model First,最近 ...
- SEC4 - MySQL语法规范
1.不区分大小写,但是建议关键字大写,表名.列名小写. 2.每条命令最好分号结尾 3.每条命令根据需要,可以进行缩进或者换行 4.注释 单行注释:#注释文字 单行注释:--注释文字 多行注释: /* ...
- hexo博客yilia主题深度设置
转载:Shuyan http://dongshuyan.com/2019/05/24/hexo博客注意事项/ 1.微信分享异常 这里是themes\yilia\layout\ _partial\pos ...
- 认识requests库,以及安装方法
1.学习requests库有什么意义 1)对我来说,我是测试人员,我用它来解决HTTP接口自动化测试 2)写爬虫需要用到requests 3)如果是开发人员,需要些接口,了解requests有助于掌握 ...
- python 转义json串
import re body=r''' {"id":${BXbiztripinfoID_10},"msg":"1234测试审批${__UUID}&qu ...
- TypeError: 'generator' object is not subscriptable
TypeError: 'generator' object is not subscriptable 生成器对象不可以带下标 def get_row(self,row_no): if not isin ...