NOIP2017最后一道题

挺难想的状压dp。

受到深度的条件限制,所以一般的状态设计带有后效性,这时候考虑把深度作为一维,这样子可以保证所有状态不重复计算一遍。

神仙预处理:先处理出一个点连到一个集合所需要的最小代价,然后再处理出一个集合连到一个集合所需要的最小代价

设$g_{s, t}$表示从s集合连到t集合的最小代价, $f_{i, j}$表示当前深度为i,挖到集合s的最小代价,有转移:

    $f_{i, s} = min(g_{s, t} * (i - 1) + f_{i - 1, t})$  t是s的子集

最后的答案  $ans = min(f_{i, maxS})$ $(0<i<n)$

可以发现这样子最优答案一定会被计算到。

时间复杂度$O(3^{n} * 2 ^ {n} * n)$.

Code:

#include <cstdio>
#include <cstring>
using namespace std; const int N = ;
const int S = ( << ) + ;
const int inf = 0x3f3f3f3f; int n, m, e[N][N], h[N][S], g[S][S], f[N][S]; inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int min(int x, int y) {
return x > y ? y : x;
} inline void chkMin(int &x, int y) {
if(y < x) x = y;
} int main() {
read(n), read(m);
memset(e, 0x3f, sizeof(e));
for(int x, y, v, i = ; i <= m; i++) {
read(x), read(y), read(v);
e[x][y] = min(e[x][y], v);
e[y][x] = min(e[y][x], v);
} /* for(int i = 1; i <= n; i++, printf("\n"))
for(int j = 1; j <= n; j++)
printf("%d ", e[i][j]); */ for(int i = ; i <= n; i++) {
for(int s = ; s < ( << n); s++) {
h[i][s] = inf;
if(!(s & ( << (i - ))))
for(int j = ; j <= n; j++)
if(s & ( << (j - )))
chkMin(h[i][s], e[i][j]);
}
} for(int s = ; s < ( << n); s++) {
for(int t = s & (s - ); t; t = s & (t - )) {
int x = s ^ t;
for(int i = ; i <= n; i++)
if(x & ( << (i - )))
g[s][t] = min(g[s][t] + h[i][t], inf);
}
} memset(f, 0x3f, sizeof(f));
for(int i = ; i <= n; i++) f[][ << (i - )] = ;
for(int i = ; i <= n; i++) {
for(int s = ; s < ( << n); s++) {
for(int t = s & (s - ); t; t = s & (t - )) {
int tmp;
if(g[s][t] != inf) tmp = g[s][t] * (i - );
else tmp = inf;
if(f[i - ][t] != inf)
chkMin(f[i][s], f[i - ][t] + tmp);
}
}
} int ans = inf;
for(int i = ; i <= n; i++)
chkMin(ans, f[i][( << n) - ]); printf("%d\n", ans);
return ;
}

Luogu 3959 [NOIP2017] 宝藏的更多相关文章

  1. Luogu 3959 [NOIP2017] 宝藏- 状压dp

    题解 真的想不到这题状压的做法...听说还有跑的飞快的模拟退火,要是现场做绝对滚粗QAQ. 不考虑深度,先预处理出 $pt_{i, S}$ 表示让一个不属于 集合 $S$ 的 点$i$ 与点集 $S$ ...

  2. Solution -「Luogu 3959」 宝藏

    果真是宝藏题目. 0x01 前置芝士 这道题我是真没往状压dp上去想.题目来源. 大概看了一下结构.盲猜直接模拟退火!\xyx 所需知识点:模拟退火,贪心. 0x02 分析 题目大意:给你一个图,可能 ...

  3. Luogu P3959 [NOIP2017]宝藏

    题目 STO rqy OTZ 首先这种题一看我们就知道可以爆搜. prim一眼假了,但是加个SA也能过. 所以我们来写状压. 记\(f_{i,j,S}\)表示起点到\(j\)距离为\(i\),我们现在 ...

  4. [luogu P3953] [noip2017 d1t3] 逛公园

    [luogu P3953] [noip2017 d1t3] 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张$N$个点$M$条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,$N ...

  5. [luogu P3960] [noip2017 d2t3] 队列

    [luogu P3960] [noip2017 d2t3] 队列 题目描述 Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Syl ...

  6. 【比赛】NOIP2017 宝藏

    这道题考试的时候就骗了部分分.其实一眼看过去,n范围12,就知道是状压,但是不知道怎么状压,想了5分钟想不出来就枪毙了状压,与AC再见了. 现在写的是状压搜索,其实算是哈希搜索,感觉状压DP理解不了啊 ...

  7. [Luogu 3952] NOIP2017 时间复杂度

    [Luogu 3952] NOIP2017 时间复杂度 一年的时间说长不长,说短,也不短. 一年之内无数次觉得难得可怕的题目,原来也就模拟这么回事儿. #include <cstdio> ...

  8. [NOIP2017]宝藏 状压DP

    [NOIP2017]宝藏 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的 m 条道路和它们的长度. 小明决心亲自前往挖 ...

  9. [NOIP2017]宝藏 子集DP

    题面:[NOIP2017]宝藏 题面: 首先我们观察到,如果直接DP,因为每次转移的代价受上一个状态到底选了哪些边的影响,因此无法直接转移. 所以我们考虑分层DP,即每次强制现在加入的点的距离为k(可 ...

随机推荐

  1. concurrent.futures模块

    1.concurrent.futures模块介绍 2.ThreadPoolExecutor线程池使用 3.ProcessPoolExecutor进程池使用 4.其他方法使用 1.concurrent. ...

  2. python知识点, float不能用 != 判断

    python知识点链接:https://github.com/taizilongxu/interview_python 搜索:python最佳事件 书单:http://lucida.me/blog/d ...

  3. python super()函数详解

    引言: 在类的多继承使用场景中,重写父类的方法时,可能会考虑到需要重新调用父类的方法,所以super()函数就是比较使用也很必要的解决方法: 文章来源: http://www.cnblogs.com/ ...

  4. 18.scrapy中selector的用法

    Selector是一个独立的模块. Selector主要是与scrapy结合使用的. 开启Scrapy shell: 1.打开命令行cmd 2.scrapy shell http://doc.scra ...

  5. ASP.net显示当前系统在线人数

    void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 Application.Lock(); if (Applic ...

  6. [PHP]用户登陆中间件

    Laravel 4中,可以使用Route::filter,而在Laravel 5中,没有了filter.php文件,官方建议使用中间件做. 下面是用户登陆的测试例子,涉及到的一些方法和使用,先参见这里 ...

  7. VB6 实现命令行调用时附着到原控制台

    Public Declare Function AttachConsole Lib "kernel32.dll" (ByVal ProcessID As Integer) As B ...

  8. CAAnimation临时取消动画,永久取消动画

    //临时取消动画 [CATransaction begin]; [CATransaction setDisableActions:YES]; mMyLayer.strokeEnd = 0; [CATr ...

  9. 登陆sharepoint的主页,提示:文件存在(异常来自 HRESULT:0x80070050)

    用sharepoint搭建了Project2007的服务器之后,由于我们公司管理域的服务器崩溃了,必须重新再加一次域,而域和服务器是关联的,即使域的名字一样,但该名字所对应的ID是不一样的,会导致一些 ...

  10. Extending Conductor

    后端 导体提供了可插拔的后端.目前的实现使用Dynomite. 每个后端需要实现4个接口: //Store for workflow and task definitions com.netflix. ...