Description
给一张网格图,有一些点必须选,必选的的店无价值,其余有价值,问把必须点全部连起来的最小代价。


Sample Input
4 4
0 1 1 0
2 5 5 1
1 5 5 1
0 1 1 0


Sample Output
6
xoox
___o
___o
xoox


斯坦纳树板子题,存一下代码,耶!


#include <queue>
#include <cstdio>
#include <cstring>using namespace std;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int _max(int x, int y) {return x > y ? x : y;}
int read() {int s = 0, f = 1; char ch = getchar();while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();return s * f;
}struct edge {int x, y, c, next;
} e[810]; int len, last[110];
int n, m, f[110][1024], from[110][1024];
int cnt, c[110];
queue<int> q;
bool v[110];int id(int x, int y) {return (x - 1) * m + y;}void ins(int x, int y) {e[++len].x = x, e[len].y = y;e[len].next = last[x], last[x] = len;
}void spfa(int S) {while(!q.empty()) {int x = q.front(); q.pop();for(int k = last[x]; k; k = e[k].next) {int y = e[k].y;if(f[y][S] > f[x][S] + c[y]) {f[y][S] = f[x][S] + c[y];from[y][S] = -x;if(!v[y]) v[y] = 1, q.push(y);}} v[x] = 0;}
}void solve() {for(int S = 1; S < (1 << cnt); S++) {memset(v, 0, sizeof(v));for(int i = 1; i <= n * m; i++) {for(int s = (S - 1) & S; s; s = (s - 1) & S) {if(f[i][S] > f[i][s] + f[i][S ^ s] - c[i]) f[i][S] = f[i][s] + f[i][S ^ s] - c[i], from[i][S] = s;} if(f[i][S] < f[0][0]) q.push(i), v[i] = 1;} spfa(S);}
}void dfs(int x, int S) {v[x] = 1;if(from[x][S] < 0) dfs(-from[x][S], S);else if(f[x][S] > 0) dfs(x, from[x][S]), dfs(x, S ^ from[x][S]);
}int main() {n = read(), m = read();memset(f, 63, sizeof(f));for(int i = 1; i <= n; i++) {for(int j = 1; j <= m; j++) {int C = read();int n1 = id(i, j); c[n1] = C;if(!C) ++cnt, f[n1][1 << cnt - 1] = 0;for(int k1 = 0; k1 < 4; k1++) {int nx = i + dx[k1], ny = j + dy[k1];if(nx <= 0 || ny <= 0 || nx > n || ny > m) continue;int n2 = id(nx, ny);ins(n1, n2);}}} solve();int x = 0, S = (1 << cnt) - 1;for(int i = 1; i <= n * m; i++) {if(f[x][S] > f[i][S]) x = i;} printf("%d\n", f[x][S]);memset(v, 0, sizeof(v));dfs(x, S);for(int i = 1; i <= n; i++) {for(int j = 1; j <= m; j++) {int hh = id(i, j);if(!c[hh]) putchar('x');else {if(v[hh]) putchar('o');else putchar('_');}} puts("");}return 0;
}

[Wc2008]游览计划 斯坦纳树相关推荐

  1. BZOJ2595: [Wc2008]游览计划(斯坦纳树,状压DP)

    Time Limit: 10 Sec  Memory Limit: 256 MBSec  Special Judge Submit: 2030  Solved: 986 [Submit][Status ...

  2. 【BZOJ 2595】2595: [Wc2008]游览计划 (状压DP+spfa,斯坦纳树?)

    2595: [Wc2008]游览计划 Time Limit: 10 Sec  Memory Limit: 256 MBSec  Special Judge Submit: 1572  Solved:  ...

  3. [WC2008]游览计划(斯坦纳树)

    [Luogu4294] 题解 : 斯坦纳树 \(dp[i][j]\) 表示以\(i\)号节点为根,当前状态为\(j\)(与\(i\)连通的点为\(1\)) 当根\(i\)不改变时状态转移方程是: \( ...

  4. 业界萌新对斯坦纳树的小结

    业界萌新对斯坦纳树的小结 0.简介 斯坦纳树问题是组合优化问题,与最小生成树相似,是最短网络的一种.最小生成树是在给定的点集和边中寻求最短网络使所有点连通.而最小斯坦纳树允许在给定点外增加额外的点,使 ...

  5. [学习笔记]斯坦纳树

    处理一种这样的问题: 斯坦纳树问题是组合优化问题,与最小生成树相似,是最短网络的一种.最小生成树是在给定的点集和边中寻求最短网络使所有点连通.而最小斯坦纳树允许在给定点外增加额外的点,使生成的最短网络 ...

  6. [THUSC2017][斯坦纳树+随机化]巧克力

    题面 [题目描述] "人生就像一盒巧克力,你永远不知道吃到的下一块是什么味道." 明明收到了一大块巧克力,里面有若干小块,排成 n n n行 m m m列.每一小块都有自己特别的图 ...

  7. P4294 [WC2008]游览计划

    题目链接 题目描述 从未来过绍兴的小D有幸参加了Winter Camp 2008,他被这座历史名城的秀丽风景所吸引,强烈要求游览绍兴及其周边的所有景点. 主办者将绍兴划分为 NNN 行 MMM 列 ( ...

  8. bzoj1402 Ticket to Ride 斯坦纳树 + 状压dp

    给定\(n\)个点,\(m\)条边的带权无向图 选出一些边,使得\(4\)对点之间可达,询问权值最小为多少 \(n \leqslant 30, m \leqslant 1000\) 首先看数据范围,\ ...

  9. BZOJ 4006 Luogu P3264 [JLOI2015]管道连接 (斯坦纳树、状压DP)

    题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4006 (luogu)https://www.luogu.org/probl ...

最新文章

  1. jsp在java软件中_5.2在JSP中使用JAVABEAN
  2. 有趣的Pycharm第三方模块——为正在学习python的可怜孩子找点乐趣
  3. 一文梳理深度学习算法演进
  4. Debian 6 7 8 utc时间设置
  5. c语言英文字符转数字,C语言常用数字和字符串转换函数(国外英文资料).doc
  6. 简述数据字典的结构及其作用_数据结构——树基本概念及其遍历
  7. Cannot find module 'express'
  8. 移除 ZooKeeper 的 kafka 2.8 ,更快了
  9. 全排列及相关扩展算法(七)——组合数的字典序(另含全章代码整理)
  10. 01c-1: 主流长远
  11. Linux用户管理详解大结局(下)
  12. 加密设备攻防(二)- 智能设备篇
  13. sstv解码_新的业余无线电 SSTV 设备已运抵国际空间站
  14. denseNet 详解
  15. android网易云桌面歌词,网易云音乐APP怎么开启桌面歌词功能
  16. ERP系统-销售子系统-销售发货通知单
  17. 苹果appstore中兑换码的使用方法
  18. css实现3D书本翻页动画
  19. 如何将华为云服务器重做系统并保留其中的指定数据
  20. 火焰课堂java_通过火焰图引入Java剖析

热门文章

  1. 微信 php 地图定位,微信公众号定位地图位置写入数据库,再显示地图的方法
  2. library netcdf 路径_科学网—NetCDF安装记录 - 丁鹏基的博文
  3. PIC16F877A单片机 (中断与定时器Timer2)
  4. UVM入门与进阶学习笔记16——sequencer和sequence(2)
  5. RabbitMQ None of the specified endpoints were reachable 错误 解决方案
  6. python 战舰_战舰python代码学院
  7. SpringCloud Tencent 全套解决方案
  8. PHP 该网页无法正常运作情况原因记录 code 500
  9. 大学生php实训总结_php实训报告心得体会
  10. PCM1863应用笔记