【题目链接】
orz凯爷,见【Lethelody的题解】
首先一个双连通图可以拆为一个小双连通图和一条链。
设c[s][u][v]表示一条链的集合状态为s,链的端点分别为u和v的最短路径。
设h[s][u][0/1]表示集合状态为s,不在集合s内的点u与另一个在集合s中的点的最短路径/次短路径。
设f[s]表示集合状态为s,且s双联通的最小权值。
c可以递推转移。h可以枚举转移。f用枚举子集+枚举两个链上的点转移。
/* Pigonometry */
#include
#include
using namespace std;
const int maxn = 13, maxm = 45, maxs = 1 << 13, inf = 1 << 28;
int n, m, head[maxn], cnt, c[maxs][maxn][maxn], h[maxs][maxn][2], f[maxs], bin[maxs], S;
struct _edge {
int u, v, w, next;
} g[maxm << 1];
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline void add(int u, int v, int w) {
g[cnt] = (_edge){u, v, w, head[u]};
head[u] = cnt++;
}
inline void init() {
for(int i = 0; i < S; i++) for(int j = 0; j <= n; j++) for(int k = 0; k <= n; k++)
c[i][j][k] = inf;
for(int i = 1; i <= n; i++) c[bin[i]][i][i] = 0;
for(int i = 0; i < cnt; i++) {
int u = g[i].u, v = g[i].v, s = bin[u] | bin[v];
c[s][u][v] = min(c[s][u][v], g[i].w);
}
for(int i = 1; i < S; 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; k = g[k].next) {
int vv = g[k].v;
if((bin[vv] | i) ^ i) {
int s = i | bin[vv];
c[s][u][vv] = min(c[s][u][vv], c[i][u][v] + g[k].w);
}
}
for(int i = 0; i < S; 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 < S; i++) for(int u = 1; u <= n; u++)
if((bin[u] | i) ^ i) for(int j = head[u]; ~j; j = g[j].next) {
int v = g[j].v;
if((bin[v] | i) == i) {
if(g[j].w <= h[i][u][0]) h[i][u][1] = h[i][u][0], h[i][u][0] = g[j].w;
else if(g[j].w < h[i][u][1]) h[i][u][1] = g[j].w;
}
}
}
inline void work() {
for(int i = 1; i < S; i++) f[i] = inf;
for(int i = 1; i <= n; i++) f[bin[i]] = 0;
for(int i = 1; i < S; i++) if(__builtin_popcount(i) >= 2)
for(int s = i & (i - 1); s; s = (s - 1) & i) {
int t = s ^ i;
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] + c[s][u][u] + h[t][u][0] + h[t][u][1]);
else f[i] = min(f[i], f[t] + c[s][u][v] + h[t][u][0] + h[t][v][0]);
}
}
}
int main() {
bin[1] = 1;
for(int i = 2; i < maxn; i++) bin[i] = bin[i - 1] << 1;
for(int T = iread(); T; T--) {
n = iread(); m = iread(); S = 1 << n;
for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0;
for(int i = 1; i <= m; i++) {
int u = iread(), v = iread(), w = iread();
add(u, v, w); add(v, u, w);
}
init();
work();
if(f[S - 1] < inf) printf("%d\n", f[S - 1]);
else printf("impossible\n");
}
return 0;
}