SNOI2013竟然出了一道很有难度的状压DP.实在是出人意料.
而且网上似乎并没有题解.我就来写一篇好了.
HYF菊苣似乎写过这道题的题解.
这道题大意就是:给出一个无向图.求一个权值最小的包含所有点的双联通子图.
定义一些状态:
f[i]:集合状态为i.且使在i中的点双联通的最小权值.
h[i][j][0]:一个端点是j.另一个端点在点集i中的边的最小权值.
h[i][j][1]:一个端点是j.另一个端点在点集i中的边的次小权值.
g[i][j][k].集合状态为i.且使在i中的点构成一条链.两端点分别是j和k的最小权值.
容易发现.一个双联通图.必然是由一个双联通的子图和一条链构成的.那么就可以枚举这条链进行转移.
要注意的是:一个点是一个权值为0的双联通图.同时也是一条权值为0的链.
在转移的时候.如果这条链是一个点.转移方程是:
f[i] = min(f[i], f[t] + g[s][u][u] + h[t][u][0] + h[t][u][1]);如果这条链的两端点不同.转移方程是:
f[i] = min(f[i], f[t] + g[s][u][v] + h[t][u][0] + h[t][v][0]);先预处理出h和m数组就好了.
关于h数组的处理.直接暴力枚举就可以了.
关于g数组的处理.也是一个状压DP.枚举当前点集i能转移出的状态更新就好了.
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <algorithm> #include <iostream> #include <fstream> #include <vector> #include <queue> #include <deque> #include <map> #include <set> #include <string> #define make make_pair #define fi first #define se second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; const int maxn = 20; const int maxm = 10100; const int maxs = 26; const int inf = 1 << 28; const int P = 1000000007; const double error = 1e-9; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch <= 47 || ch >= 58) f = (ch == 45 ? -1 : 1), ch = getchar(); while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar(); return x * f; } struct edge { int u, v, w, next; } e[maxm]; int n, m, cnt, head[maxn], bin[maxn], f[maxm], g[maxm][maxn][maxn], h[maxm][maxn][2]; void insert(int u, int v, int w) { e[cnt] = (edge) {u, v, w, head[u]}, head[u] = cnt++; e[cnt] = (edge) {v, u, w, head[v]}, head[v] = cnt++; } int fac(int x) { int ans = 1; for (x = x & (x - 1); x; x = x & (x - 1)) ans++; return ans; } void init() { for (int i = 0; i < (1 << n); i++) for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) g[i][j][k] = inf; for (int i = 1; i <= n; i++) { bin[i] = 1 << (i - 1); g[bin[i]][i][i] = 0; } for (int i = 0; i < cnt; i++) { int u = e[i].u, v = e[i].v, s = bin[u] + bin[v]; g[s][u][v] = min(g[s][u][v], e[i].w); } for (int i = 1; i < (1 << n); i++) for (int u = 1; u <= n; u++) for (int v = 1; v <= n; v++) if ((bin[u] | i) == i && (bin[v] | i) == i) for (int k = head[v]; k != -1; k = e[k]. next) { int fv = e[k].v; if ((bin[fv] | i) != i) { int s = i + bin[fv]; g[s][u][fv] = min(g[s][u][fv], g[i][u][v] + e[k].w); } } for (int i = 0; i < (1 << n); i++) for (int j = 0; j <= n; j++) for (int k = 0;k <= 1; k++) h[i][j][k] = inf; for (int i = 1; i < (1 << n); i++) for (int u = 1; u <= n; u++) if ((bin[u] | i) != i) for (int j = head[u]; j != -1; j = e[j].next) { int v = e[j].v; if ((bin[v] | i) == i) { if (e[j].w <= h[i][u][0]) { h[i][u][1] = h[i][u][0]; h[i][u][0] = e[j].w; } else if (e[j].w < h[i][u][1]) h[i][u][1] = e[j].w; } } } int work() { for (int i = 1; i < (1 << n); i++) f[i] = inf; for (int i = 1; i <= n; i++) f[bin[i]] = 0; for (int i = 1; i < (1 << n); i++) if (fac(i) >= 2) for (int s = i & (i - 1); s; s = (s - 1) & i) { int t = i - s; for (int u = 1; u <= n; u++) for (int v = 1; v <= n; v++) if ((bin[u] | s) == s && (bin[v] | s) ==s) { if (u == v) f[i] = min(f[i], f[t] + g[s][u][u] + h[t][u][0] + h[t][u][1]); else f[i] = min(f[i], f[t] + g[s][u][v] + h[t][u][0] + h[t][v][0]); } } } int main() { int t = read(); while (t--) { n = read(), m = read(); memset(head, - 1, sizeof head); cnt = 0; for (int i = 1; i <= m; i++) { int u = read(), v = read(), w = read(); insert(u, v, w); } init(), work(); if (f[(1 << n) - 1] < inf) printf("%d\n", f[(1 << n) - 1]); else printf("impossible\n"); } return 0; }