#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef pair pii; template inline void chkmin(T &x, U y) { if(y < x) x = y; } template inline void chkmax(T &x, U y) { if(x < y) x = y; } int get() { char c; while (c = getchar(), (c < '0' || c > '9') && (c != '-')); bool flag = (c == '-'); if (flag) c = getchar(); int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + c - 48; c = getchar(); } return flag ? -x : x; } #define MX 17 #define INF 123456789 int n, m; int dis[MX][MX]; int val[(1 << MX) + 6][MX + 7], chk[(1 << MX) + 6][MX + 7], T; int calc(int u, int pos) { if (chk[u][pos] == T) return val[u][pos]; int& ret = val[u][pos]; chk[u][pos] = T; ret = INF; int flag = 0; for (int i = 0; i < n - 1; i++) { if (u & (1 << i)) { flag = 1; chkmin(ret, dis[pos][i + 2] + calc(u ^ (1 << i), i + 2)); } } if (flag == 0) { ret = dis[pos][1]; } return ret; } int main() { int Tcase; for (scanf("%d", &Tcase); Tcase--;) { fill(dis[0], dis[0] + MX * MX, INF); //scanf("%d%d", &n, &m); T++; n = get(), m = get(); while (m--) { int u, v, w; //scanf("%d%d%d", &u, &v, &w); u = get(), v = get(), w = get(); chkmin(dis[u][v], w); chkmin(dis[v][u], w); } if (n == 1) { printf("%d\n", 0); continue; } for (int i = 1; i <= n; i++) dis[i][i] = 0; for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { chkmin(dis[i][j], dis[i][k] + dis[k][j]); } } } printf("%d\n", calc((1<<(n - 1)) - 1, 1)); } return 0; }