int pow3[maxn], tri[maxm][maxn]; voidinit(){ pow3[0] = 1; for (int i = 1; i < maxn; ++i) pow3[i] = pow3[i - 1] * 3; for (int i = 0; i < maxm; ++i) { int st = i; for (int j = 1; j < maxn; ++j) { tri[i][j - 1] = st % 3; st /= 3; } } }
int f[maxm][maxn];
voidwork(){ int M = pow3[n] - 1; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) g[i][j] = INF; for (int i = 0; i <= M; ++i) for (int j = 1; j <= n; ++j) f[i][j] = INF; for (int i = 1; i <= m; ++i) { int x, y, z; cin >> x >> y >> z; g[x][y] = g[y][x] = min(g[x][y], z); } for (int i = 1; i <= n; ++i) f[pow3[i - 1]][i] = 0; for (int S = 0; S <= M; ++S) for (int i = 1; i <= n; ++i) { if (!tri[S][i - 1]) continue; for (int j = 1; j <= n; ++j) { if (tri[S][j - 1] == 2) continue; f[S + pow3[j - 1]][j] = min(f[S + pow3[j - 1]][j], f[S][i] + g[i][j]); } } int ans = INF; for (int S = 1; S <= M; ++S) { bool F = 1; for (int i = 1; i <= n; ++i) if (!tri[S][i - 1]) F = 0; if (!F) continue; for (int i = 1; i <= n; ++i) ans = min(ans, f[S][i]); } if (ans == INF) cout << -1 << endl; elsecout << ans << endl; }
intmain(){ init(); while (cin >> n >> m) work(); return0; }