Description

题目链接

求一张无向带权图的边双连通生成子图的最小代价。

Solution

核心的思路是,一个点双连通分量肯定是一堆环的并。

考虑增量地构造这个边双连通图,每次把一个环并进去,相当于加入了一条链。

那么这个转移需要:原集合的代价,链的代价,链的端点连入集合的代价。

设 \(A\) 为新图点集,\(S\) 为原图点集,设 \(f[S]\) 表示点集 \(S\) 构成边双连通分量的最小代价。

设 \(T\) 为新加入链的点集,\(u,v\) 分别为加入的链的端点,设 \(g[u][v][T]\) 表示该链的最小代价。

设 \(mm[u][S]\) 表示点 \(u\) 向集合 \(S\) 中的点所连边中,边权最小值。

\[f[A]=f[S]+g[u][v][T]+mn[u][S]+mn[v][S]
\]

但是注意,如果新加入的链退化成了一个点,加入的代价就算少了。

因此设 \(sec[u][S]\) 表示点 \(u\) 向集合 \(S\) 中的点所连边中,边权次小值。

那么对于 \(u=v\) 的情况:

\[f[A]=f[S]+g[u][u][T]+mn[u][S]+sec[u][S]
\]

预处理 \(mn\) 和 \(sec\) 复杂度 \(\mathcal O(n^2\times 2^n)\)

预处理 \(g\) 暴力枚举一个端点的变化,复杂度 \(\mathcal O(n^3\times 2^n)\)

计算 \(f\) 需要枚举子集,然后枚举 \(u, v\) ,复杂度 \(\mathcal O(n^2\times 3^n )\)

  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cctype>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <iostream>
  7. #include <algorithm>
  8. #define N 15
  9. #define M 105
  10. #define S 4105
  11. using namespace std;
  12. inline int rd() {
  13. int x = 0;
  14. char c = getchar();
  15. while (!isdigit(c)) c = getchar();
  16. while (isdigit(c)) {
  17. x = x * 10 + (c ^ 48); c = getchar();
  18. }
  19. return x;
  20. }
  21. int n, m, tot, lim, hd[N];
  22. struct edge{int w, to, nxt;} e[M << 1];
  23. inline void add(int u, int v, int w) {
  24. e[++tot].to = v; e[tot].w = w;
  25. e[tot].nxt = hd[u]; hd[u] = tot;
  26. }
  27. //mn[i][S]: i 到 S 最短路
  28. //sec[i][S]: i 到 S 次短路
  29. //g[i][j][S]: 一条链,节点集合为 S, 端点分别为 i, j
  30. //f[S]: 集合为 S 的合法方案
  31. int f[S], g[N][N][S], mn[N][S], sec[N][S];
  32. inline void mmin(int &x, int y) {x = min(x, y);}
  33. inline int countbit(int s) {
  34. int res = 0;
  35. for (int i = 0; i < n; ++i)
  36. res += ((s & (1 << i)) > 0);
  37. return res;
  38. }
  39. inline void work() {
  40. n = rd(); m = rd();
  41. tot = 0; lim = (1 << n);
  42. for (int i = 0; i <= n; ++i) hd[i] = 0;
  43. for (int i = 1, u, v, w; i <= m; ++i) {
  44. u = rd() - 1; v = rd() - 1; w = rd();
  45. add(u, v, w); add(v, u, w);
  46. }
  47. memset(f, 0x1f, sizeof(f));
  48. memset(g, 0x1f, sizeof(g));
  49. memset(mn, 0x1f, sizeof(mn));
  50. memset(sec, 0x1f, sizeof(sec));
  51. int inf = f[0];
  52. //处理 mn 和 sec
  53. for (int s = 1; s < lim; ++s)
  54. for (int u = 0; u < n; ++u)
  55. if ((s & (1 << u)) == 0)
  56. for (int i = hd[u], v; i; i = e[i].nxt) {
  57. v = e[i].to;
  58. if ((s & (1 << v)) == 0) continue;
  59. if (e[i].w < mn[u][s]) {
  60. sec[u][s] = mn[u][s];
  61. mn[u][s] = e[i].w; continue;
  62. } else sec[u][s] = min(sec[u][s], e[i].w);
  63. }
  64. //处理 g
  65. for (int u = 0; u < n; ++u) g[u][u][1 << u] = 0;
  66. for (int s = 1; s < lim; ++s)
  67. for (int u = 0; u < n; ++u)
  68. for (int x = 0; x < n; ++x)
  69. if (g[u][x][s] < inf)
  70. for (int i = hd[u], v; i; i = e[i].nxt) {
  71. v = e[i].to;
  72. if (s & (1 << v)) continue;
  73. mmin(g[v][x][s | (1 << v)], g[u][x][s] + e[i].w);
  74. }
  75. //处理 f
  76. for (int u = 0; u < n; ++u) f[1 << u] = 0;
  77. for (int nw = 1; nw < lim; ++nw)
  78. if (countbit(nw) >= 2) {
  79. for (int s = nw & (nw - 1); s; s = (s - 1) & nw) {
  80. int t = nw - s;
  81. for (int u = 0; u < n; ++u)
  82. if (s & (1 << u)) for (int v = 0; v < n; ++v)
  83. if (s & (1 << v) && g[u][v][s] < inf) {
  84. if (u == v) f[nw] = min(f[nw], f[t] + g[u][v][s] + mn[u][t] + sec[u][t]);
  85. else f[nw] = min(f[nw], f[t] + g[u][v][s] + mn[u][t] + mn[v][t]);
  86. }
  87. }
  88. }
  89. if (f[lim - 1] == inf) puts("impossible");
  90. else printf("%d\n", f[lim - 1]);
  91. }
  92. int main() {
  93. int testcase = rd();
  94. while (testcase--) work();
  95. return 0;
  96. }

[ SNOI 2013 ] Quare的更多相关文章

  1. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  2. SharePoint 2013: A feature with ID has already been installed in this farm

    使用Visual Studio 2013创建一个可视web 部件,当右击项目选择"部署"时报错: "Error occurred in deployment step ' ...

  3. Visual Studio 2013 添加一般应用程序(.ashx)文件到SharePoint项目

    默认,在用vs2013开发SharePoint项目时,vs没有提供一般应用程序(.ashx)的项目模板,本文解决此问题. 以管理员身份启动vs2013,创建一个"SharePoint 201 ...

  4. SharePoint 2013 create workflow by SharePoint Designer 2013

    这篇文章主要基于上一篇http://www.cnblogs.com/qindy/p/6242714.html的基础上,create a sample workflow by SharePoint De ...

  5. Install and Configure SharePoint 2013 Workflow

    这篇文章主要briefly introduce the Install and configure SharePoint 2013 Workflow. Microsoft 推出了新的Workflow ...

  6. SharePoint 2013 configure and publish infopth

    This article will simply descript how to configure and publish a InfoPath step by step. Note: To con ...

  7. TFS 2013 培训视频

    最近给某企业培训了完整的 TFS 2013 系列课程,一共四天. 下面是该课程的内容安排: 项目管理     建立项目     成员的维护     Backlog 定义     任务拆分     迭代 ...

  8. Visual Studio 2013 Ultimate因为CodeLens功能导致Microsoft.Alm.Shared.Remoting.RemoteContainer.dll高CPU占用率的折中解决方案

    1.为什么Microsoft.Alm.Shared.Remoting.RemoteContainer.dll的CPU占用率以及内存使用率会那么高? 在Visual Studio 2013 Ultima ...

  9. 沙盒解决方案解决SharePoint 2013 以其他身份登陆的问题

    众所周知,SharePoint 2013没有像SharePoint 2010那样有一个叫"以其他身份登录"的菜单项. 当然解决方案也很多,比如你可以直接修改Welcome.ascx ...

随机推荐

  1. HDU3533 Escape —— BFS / A*算法 + 预处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 Escape Time Limit: 20000/10000 MS (Java/Others)  ...

  2. 前端如何展示商品属性:SKU多维属性状态判断算法的应用-Vue 实现

    由于公司开发了一个电商项目,涉及到前台商品属性的展示,所以百度上找了一下!找到了 周琪力写的一个算法例子,因为作者只有jQuery 实现demo, 自己仿照 demo 实现了一个 vue 的! 周琪力 ...

  3. 动态的添加ImageView到LinearLayout中并居中显示

    ImageView imageView = new ImageView(mActivity); imageView.setImageResource(R.mipmap.gengduo); Linear ...

  4. 一步一步学Silverlight 2系列(9):使用控件模板

    述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  5. hdu2552

    点击打开链接 思路: 1.tan(a+b) = ( tan(a) + tan(b) ) / (1 – tan(a) * tan(b) ) 2.tan( atan(x) ) = x arctan(1/s ...

  6. Unity5.6打包问题

    将unity切换到5.6版本后打Android包时,提示android sdk tools version低于compile version,于是更新了android-sdk(下载了Android-S ...

  7. boost之timer

    1. timer类实现 #pragma once #include <ctime> #include <limits> class timer { public: timer( ...

  8. NSArray使用须知

    多用firstObject方法 在iOS7之前,我们获取NSArray的第一个元素,通常使用array[0],在iOS7中,新引入了公开的firstObject方法,对于空数组,该方法返回nil,而使 ...

  9. 移植tslib库出现selected device is not a touchscreen I understand的解决方法

    首发平台:微信公众号baiwenkeji 很多人在做触摸屏驱动实验,移植tslib库时,可能会出现错误提示“selected device is not a touchscreen I underst ...

  10. web_html-day1

    概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...