bzoj3836
状压dp
图上怎么跑dp?我们跑三进制状压dp,0表示选了,1表示既没选也没覆盖,2表示没选但是被覆盖了。
状态是dp[dep][S]表示当前走到了深度为dep的节点,状态为S,按照dfs序转移
每次转移就是计算这个点选了没选,然后像树形dp一样更新节点
返祖边也要处理
#include<bits/stdc++.h>
using namespace std;
const int N = 5e4 + ;
int n, m, ans;
vector<int> G[N];
int dp[][N], c[N], vis[N], bin[], st[N], d[N];
inline int rd()
{
int x = , f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = x * + c - ''; c = getchar(); }
return x * f;
}
int bit(int S, int t)
{
return S / bin[t] % ;
}
void dfs(int u, int dep)
{
vis[u] = ;
d[u] = dep;
if(!dep)
{
dp[][] = c[u];
dp[][] = ;
dp[][] = 1e9;
}
else
{
int top = ;
for(int i = ; i < G[u].size(); ++i)
{
int v = G[u][i];
if(d[v] < d[u] && vis[v]) st[++top] = d[v];
}
for(int i = ; i < bin[dep + ]; ++i) dp[dep][i] = 1e9;
for(int i = ; i < bin[dep]; ++i)
{
int U = , V = i;
for(int j = ; j <= top; ++j) if(bit(i, st[j]) == ) U = ; else if(bit(i, st[j]) == ) V += bin[st[j]];
dp[dep][i + U * bin[dep]] = min(dp[dep][i + U * bin[dep]], dp[dep - ][i]);
dp[dep][V] = min(dp[dep][V], dp[dep - ][i] + c[u]);
}
}
for(int i = ; i < G[u].size(); ++i)
{
int v = G[u][i];
if(vis[v]) continue;
dfs(v, dep + );
for(int j = ; j < bin[dep + ]; ++j) dp[dep][j] = min(dp[dep + ][j], dp[dep + ][j + * bin[dep + ]]);
}
}
int main()
{
n = rd();
m = rd();
bin[] = ;
for(int i = ; i <= ; ++i) bin[i] = bin[i - ] * ;
for(int i = ; i <= n; ++i) c[i] = rd();
for(int i = ; i <= m; ++i)
{
int u = rd(), v = rd();
G[u].push_back(v);
G[v].push_back(u);
}
for(int i = ; i <= n; ++i) if(!vis[i])
{
dfs(i, );
ans += min(dp[][], dp[][]);
}
printf("%d\n", ans);
return ;
}
bzoj3836的更多相关文章
- BZOJ3836 [Poi2014]Tourism 【树形dp +状压dp】
题目链接 BZOJ3836 题解 显然这是个\(NP\)完全问题,此题的解决全仗任意两点间不存在节点数超过10的简单路径的性质 这意味着什么呢? \(dfs\)树深度不超过\(10\) \(10\)很 ...
- BZOJ3836 : [Poi2014]Tourism
对于一个连通块,取一个点进行dfs,得到一棵dfs搜索树,则这棵树的深度不超过10,且所有额外边都是前向边. 对于每个点x,设S为三进制状态,S第i位表示根到x路径上深度为i的点的状态: 0:选了 1 ...
- OI动态规划&&优化 简单学习笔记
持续更新!! DP的难点主要分为两类,一类以状态设计为难点,一类以转移的优化为难点. DP的类型 序列DP [例题]BZOJ2298 problem a 数位DP 常用来统计或者查找一个区间满足条件的 ...
- POI2014题解
POI2014题解 [BZOJ3521][Poi2014]Salad Bar 把p当作\(1\),把j当作\(-1\),然后做一遍前缀和. 一个合法区间\([l,r]\)要满足条件就需要满足所有前缀和 ...
随机推荐
- Terminal emulator
http://en.wikipedia.org/wiki/Terminal_emulator Terminal emulator From Wikipedia, the free encycloped ...
- POJ 3335 Rotating Scoreboard(半平面交 多边形是否有核 模板)
题目链接:http://poj.org/problem? id=3335 Description This year, ACM/ICPC World finals will be held in a ...
- Apache Server与多个独立Tomcat集成
取经自http://www.ramkitech.com/2012/03/virtual-host-apache-httpd-server-tomcat.html 继续干Tomcat和Apache Se ...
- Web性能测试工具:Siege安装&使用简介
在Web性能测试工具中,siege是比较热门和常见的,它有安装简单,使用简单,测试报告详细的特点. 并且可以在文本中预定义一系列待测试url模拟,并可设定一定并发量下持续指定时间or测试进行测试. 比 ...
- Linux命令之ln软链接
用途:链接文件 默认情况下,ln命令产生硬链接. 最常用的参数是-s(建立符号连接Symbolic Link,也叫软连接),具体用法是: ln-s 源文件 目标文件 当我们需要在不同的目录用到相同的文 ...
- TEA对称加密算法
今天在看<Distributed Systems Concepts and Design>这本书的时候,在讲到分布式系统的安全性的时候,给出了TEA算法,书本上有现成的代码,所以摘录下来以 ...
- send data to Flume client-sdk flume使用之httpSource
https://flume.apache.org/FlumeDeveloperGuide.html#client-sdk flume使用之httpSource - CSDN博客 https://blo ...
- The JSP specification requires that an attribute name is preceded by whitespace--异常
异常信息:org.apache.jasper.JasperException: /pages/selectedCourse.jsp (line: 4, column: 39) The JSP spec ...
- Windows踩坑笔记之使用_tWinMain报错的解决方案
对于如下代码 #include <Windows.h> int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, ...
- 稳定币GUSD的优劣势分析
在币圈,有人乘着牛市东风一夜暴富,也有人不幸赶上熊市倾家荡产,涨跌大起大落是币圈的常态,在如此不稳定的币市,投资者们都想寻求一些稳定.接着,稳定币诞生了. 2018年下半年,稳定币引起了各路投资者的高 ...