斯坦纳树 [bzoj2595][wc2008]游览计划 题解
话说挺早就写过斯坦纳树了,不过当时没怎么总结,也不是很理解……现在来个小结吧~
斯坦纳树就是包含给定点的最小生成树(个人理解权值应当为正)。
一般来讲,给定点的数目应该很小吧。。。于是我们可以用状压DP来解决。
需要2个方程:
f[st][i]表示连通性至少为st,且经过i点的最小距离
方程1.f[st][i] = Min{f[s][i] + f[st - s][i]}(s为st的子集)
方程2.f[st][i] = Min{f[st][j] + w(i,j)}(i,j之间有边相连)
这里大家可能会有疑问,为什么是两个方程?
因为单凭第一个方程转移是不够准确的,会出现重点的问题。不过也一定包含合法的转移,所以我们要通过第二个方程来转移。如果我没有理解错,这也就是为什么这种方法不能应用于有负权的图上。
第一个直接枚举子集,完了以后用SPFA转移下一个(当然也可以用floyed)。
void SPFA(int sta)
{
while (!q.empty()) {
int i,j;
unpack(q.front(),i,j);
inq[q.front()] = 0;q.pop();
for (int k = 0; k < 4; ++k) {
int x = d[k][0] + i,y = d[k][1] + j,tmp;
if (x == -1 || y == -1 || x == n || y == m) continue;
if (update(x,y,sta,i,j,sta,f[i][j][sta] + a[x][y]) && !inq[tmp = pack(x,y)])
q.push(tmp),inq[tmp] = 1;
}
}
}
要记住f[st][i]表示连通性至少为st,是至少。
for (int sta = 1,tmp; sta < Max_s; ++sta) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
for (int s = sta&(sta - 1); s; s = (s - 1)&sta)
update(i,j,sta,i,j,s,f[i][j][s] + f[i][j][sta - s] - a[i][j]);
if (f[i][j][sta] != INF) q.push(tmp = pack(i,j)),inq[tmp] = 1;
}
SPFA(sta);
}
这样转移就可以了。
/**************************************************************
Problem: 2595
User: lazycal
Language: C++
Result: Accepted
Time:144 ms
Memory:1640 kb
****************************************************************/
#include <cstdio>
#include <cstring>
#include <queue>
using std::queue;
queue<int>q;
const int N = 10,INF = 0xf0f0f0f;
const int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int f[N][N][1<<N],n,m,a[N][N],st[N][N],K,pre[N][N][1<<N];
bool vis[N][N],inq[N*N];
int pack(const int x,const int y){return x*10 + y;}
int pack2(const int x,const int y,const int s){return x*100000 + y*10000 + s;}
void unpack(const int x,int &i,int &j){i = x/10; j = x%10;}
void unpack2(const int x,int &i,int &j,int &s){s = x%10000; j = (x/10000)%10; i = x/100000;}
bool update(const int x,const int y,const int news,const int i,const int j,const int sta,const int w)
{
if (f[x][y][news] > w) return f[x][y][news] = w,pre[x][y][news] = pack2(i,j,sta),true;
return false;
}
void SPFA(int sta)
{
while (!q.empty()) {
int i,j;
unpack(q.front(),i,j);
inq[q.front()] = 0;q.pop();
for (int k = 0; k < 4; ++k) {
int x = d[k][0] + i,y = d[k][1] + j,tmp;
if (x == -1 || y == -1 || x == n || y == m) continue;
if (update(x,y,sta/*|st[x][y]*/,i,j,sta,f[i][j][sta] + a[x][y]) /*&& (sta|st[x][y]) == sta*/ && !inq[tmp = pack(x,y)])
q.push(tmp),inq[tmp] = 1;
}
}
}
void dfs(const int i,const int j,const int s)
{
if (!pre[i][j][s]) return;
int x,y,ns;
vis[i][j] = 1;
unpack2(pre[i][j][s],x,y,ns);
dfs(x,y,ns);
if (x == i && y == j) dfs(x,y,s - ns);
}
void output()
{
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
if (!a[i][j]) printf("x");
else if (vis[i][j]) printf("o");
else printf("_");
puts("");
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("2595.in","r",stdin);
freopen("2595.out","w",stdout);
#endif
scanf("%d%d",&n,&m);
memset(f,0xf,sizeof f);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
scanf("%d",&a[i][j]);
if (!a[i][j]) st[i][j] = 1<<(K++),f[i][j][st[i][j]] = 0;
}
int Max_s = (1 << K);
for (int sta = 1,tmp; sta < Max_s; ++sta) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
//if (a[i][j] && !(st[i][j]&sta)) continue;
for (int s = sta&(sta - 1); s; s = (s - 1)&sta)
update(i,j,sta,i,j,s,f[i][j][s] + f[i][j][sta - s] - a[i][j]);
if (f[i][j][sta] != INF) q.push(tmp = pack(i,j)),inq[tmp] = 1;//test!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
SPFA(sta);
// printf("\n%d\n",sta);
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < m; ++j) printf("%d ",f[i][j][sta]);
// puts("");
// }
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (!a[i][j]) {
printf("%d\n",f[i][j][Max_s - 1]);
dfs(i,j,Max_s - 1);
output();
return 0;
}
}
斯坦纳树 [bzoj2595][wc2008]游览计划 题解的更多相关文章
- BZOJ2595 Wc2008 游览计划 【斯坦纳树】【状压DP】*
BZOJ2595 Wc2008 游览计划 Description Input 第一行有两个整数,N和 M,描述方块的数目. 接下来 N行, 每行有 M 个非负整数, 如果该整数为 0, 则该方块为一个 ...
- [bzoj2595][WC2008]游览计划/[bzoj5180][Baltic2016]Cities_斯坦纳树
游览计划 bzoj-2595 wc-2008 题目大意:题目链接.题目连接. 注释:略. 想法:裸题求斯坦纳树. 斯坦纳树有两种转移方式,设$f[s][i]$表示联通状态为$s$,以$i$为根的最小代 ...
- BZOJ2595 WC2008游览计划(斯坦纳树)
斯坦纳树板子题. 考虑状压dp,设f[i][j][S]表示当前在点(i,j)考虑转移,其所在的联通块包含的关键点集(至少)为S的答案. 转移时首先枚举子集,有f[i][j][S]=min{f[i][j ...
- bzoj2595 [Wc2008]游览计划——斯坦纳树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2595 今天刚学了斯坦纳树,还不太会,写一道题练习一下: 参考了博客:http://www.c ...
- bzoj2595: [Wc2008]游览计划 斯坦纳树
斯坦纳树是在一个图中选取某些特定点使其联通(可以选取额外的点),要求花费最小,最小生成树是斯坦纳树的一种特殊情况 我们用dp[i][j]来表示以i为根,和j状态是否和i联通,那么有 转移方程: dp[ ...
- BZOJ2595: [Wc2008]游览计划(斯坦纳树,状压DP)
Time Limit: 10 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 2030 Solved: 986[Submit][Status][ ...
- BZOJ2595 [Wc2008]游览计划 【状压dp + 最短路】
题目链接 BZOJ2595 题解 著名的斯坦纳树问题 设\(f[i][j][s]\)表示点\((i,j)\)与景点联通状况为\(s\)的最小志愿者数 设\(val[i][j]\)为\((i,j)\)需 ...
- BZOJ2595[WC2008]游览计划
Description Input 第一行有两个整数,N和 M,描述方块的数目. 接下来 N行, 每行有 M 个非负整数, 如果该整数为 0, 则该方块为一个景点:否则表示控制该方块至少需要的志愿者数 ...
- BZOJ2595:[Wc2008]游览计划——题解(插头dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=2595 Description Input 第一行有两个整数,N和 M,描述方块的数目. 接下来 N行 ...
随机推荐
- 51nod 1258 序列求和 V4
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1258 1258 序列求和 V4 基准时间限制:8 秒 空间限制:131 ...
- Ionic -- css
Header 固定在头部,可以包含标题标签,可以有左右按钮 样式:bar bar-header bar-light 第一个小节 第二个表示的是头部 第三个表示颜色 子头部,需要在ion-content ...
- Anaconda+django写出第一个web app(十)
今天继续学习外键的使用. 当我们有了category.series和很多tutorials时,我们查看某个tutorial,可能需要这样的路径http://127.0.0.1:8000/categor ...
- flask基础之请求钩子(十二)
前言 什么是请求钩子?在客户端和服务器交互的过程中,有些准备工作或扫尾工作需要统一处理,为了让每个视图函数避免编写重复功能的代码,flask提供了统一的接口可以添加这些处理函数,即请求钩子. 请求钩子 ...
- java 异常匹配
抛出异常的时候,异常处理系统会安装代码书写顺序找出"最近"的处理程序. 找到匹配的程序后,它就认为异常将得到清理,然后就不再继续查找. 查找的时候并不要求抛出的异常同处理程序的异常 ...
- CF1064A 【Make a triangle!】
要让这个三角形合法,只需满足三角形不等式 即$a+b>c$,设$c=max\left\{a,b,c\right\}$,上式转化为$c<a+b$ 如果已经满足,不需消耗代价 否则消耗$c-a ...
- SqlServer共用表达式(CTE)With As
共用表表达式(CTE)可以看成是一个临时的结果集,可以再SELECT,INSERT,UPDATE,DELETE,MARGE语句中多次引用. 一好处:使用共用表表达式可以让语句更加清晰简练. 1.可以定 ...
- Java web项目使用webSocket
前端: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&qu ...
- Java编程的逻辑 (27) - 剖析包装类 (中)
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- AndroidManifest.xml中android:configChanges的简介
程序在运行时,一些设备的配置可能会改变,如:横竖屏的切换.键盘的可用性等,这样的事情一发生,Activity会重新启动,其中的过程是:在销毁之前会先 called onSaveInstanceStat ...