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 )\)

#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 15
#define M 105
#define S 4105
using namespace std; inline int rd() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
x = x * 10 + (c ^ 48); c = getchar();
}
return x;
} int n, m, tot, lim, hd[N]; struct edge{int w, to, nxt;} e[M << 1]; inline void add(int u, int v, int w) {
e[++tot].to = v; e[tot].w = w;
e[tot].nxt = hd[u]; hd[u] = tot;
} //mn[i][S]: i 到 S 最短路
//sec[i][S]: i 到 S 次短路
//g[i][j][S]: 一条链,节点集合为 S, 端点分别为 i, j
//f[S]: 集合为 S 的合法方案 int f[S], g[N][N][S], mn[N][S], sec[N][S]; inline void mmin(int &x, int y) {x = min(x, y);} inline int countbit(int s) {
int res = 0;
for (int i = 0; i < n; ++i)
res += ((s & (1 << i)) > 0);
return res;
} inline void work() {
n = rd(); m = rd();
tot = 0; lim = (1 << n);
for (int i = 0; i <= n; ++i) hd[i] = 0;
for (int i = 1, u, v, w; i <= m; ++i) {
u = rd() - 1; v = rd() - 1; w = rd();
add(u, v, w); add(v, u, w);
}
memset(f, 0x1f, sizeof(f));
memset(g, 0x1f, sizeof(g));
memset(mn, 0x1f, sizeof(mn));
memset(sec, 0x1f, sizeof(sec));
int inf = f[0];
//处理 mn 和 sec
for (int s = 1; s < lim; ++s)
for (int u = 0; u < n; ++u)
if ((s & (1 << u)) == 0)
for (int i = hd[u], v; i; i = e[i].nxt) {
v = e[i].to;
if ((s & (1 << v)) == 0) continue;
if (e[i].w < mn[u][s]) {
sec[u][s] = mn[u][s];
mn[u][s] = e[i].w; continue;
} else sec[u][s] = min(sec[u][s], e[i].w);
}
//处理 g
for (int u = 0; u < n; ++u) g[u][u][1 << u] = 0;
for (int s = 1; s < lim; ++s)
for (int u = 0; u < n; ++u)
for (int x = 0; x < n; ++x)
if (g[u][x][s] < inf)
for (int i = hd[u], v; i; i = e[i].nxt) {
v = e[i].to;
if (s & (1 << v)) continue;
mmin(g[v][x][s | (1 << v)], g[u][x][s] + e[i].w);
}
//处理 f
for (int u = 0; u < n; ++u) f[1 << u] = 0;
for (int nw = 1; nw < lim; ++nw)
if (countbit(nw) >= 2) {
for (int s = nw & (nw - 1); s; s = (s - 1) & nw) {
int t = nw - s;
for (int u = 0; u < n; ++u)
if (s & (1 << u)) for (int v = 0; v < n; ++v)
if (s & (1 << v) && g[u][v][s] < inf) {
if (u == v) f[nw] = min(f[nw], f[t] + g[u][v][s] + mn[u][t] + sec[u][t]);
else f[nw] = min(f[nw], f[t] + g[u][v][s] + mn[u][t] + mn[v][t]);
}
}
}
if (f[lim - 1] == inf) puts("impossible");
else printf("%d\n", f[lim - 1]);
} int main() {
int testcase = rd();
while (testcase--) work();
return 0;
}

[ 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. 传统maven项目创建

    转自:https://blog.csdn.net/wangfengtong/article/details/77098238 需求表均同springmvc案例 此处只是使用maven 注意,以下所有需 ...

  2. (linux)tasklet

      tasklet Tasklet的使用比较简单,只需要定义tasklet及其处理函数并将两者关联 例子: Void my_tasklet_func(unsigned long) DECLARE_TA ...

  3. (linux)schedule_delayed_work()

      原文地址:schedule_delayed_work()用法作者:Valley   第一篇 工作队列       在Linux内核中,对下半部(或者说推后执行的工作)的处理方式有好几种,包括BH( ...

  4. 基于Vuejs的搜索匹配功能

    最近一直在看vue,查了很多资料,看了很多文档和博客,大概半知半解了,然后利用所理解的知识写了一个简单的搜索匹配功能. 大概长这个样子:   <!DOCTYPE html> <htm ...

  5. 非常精彩的Silverlight 2控件样式

    概述 大家是否觉的现在Silverlight 2提供的默认的控件不能满足自己的要求?好在Silverlight的控件可以运用皮肤,微软Silverlight控件的设计者的主管Corrina开发了几套非 ...

  6. 牛人的ACM经验 (转)

    一:知识点     数据结构:       1,单,双链表及循环链表       2,树的表示与存储,二叉树(概念,遍历)二叉树的                    应用(二叉排序树,判定树,博弈 ...

  7. Android自动化测试环境搭建

    Android自动化环境的搭建主要包括: 1. java jdk和jre的安装和环境的配置 2. appium服务器的安装和配置 3. eclipse开发工具,这里不必要用Android Studio ...

  8. Rational Rose 2007 &Rational Rose 2003 下载及破解方法和汉化文件下载

    Rational Rose 2007 &Rational Rose 2003 下载及破解方法和汉化文件下载 分类: 其它2010-08-10 13:28 12926人阅读 评论(4) 收藏 举 ...

  9. innobackupex参数说明

    1.备份: #常用参数 --user:该选项表示备份账号. --password:该选项表示备份的密码. --port:该选项表示备份数据库的端口. --host:该选项表示备份数据库的地址. --s ...

  10. 网络爬虫之requests模块的使用+Github自动登入认证

    本篇博客将带领大家梳理爬虫中的requests模块,并结合Github的自动登入验证具体讲解requests模块的参数. 一.引入:   我们先来看如下的例子,初步体验下requests模块的使用: ...