http://ac.jobdu.com/problem.php?pid=1430
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <algorithm> #include <vector> #include <cstring> #include <stack> #include <cctype> #include <utility> #include <map> #include <string> #include <climits> #include <set> #include <string> #include <sstream> #include <utility> #include <ctime> #include <bitset> #include <iomanip> //#pragma comment(linker, "/STACK:102400000,102400000") using std::priority_queue; using std::vector; using std::swap; using std::stack; using std::sort; using std::max; using std::min; using std::pair; using std::map; using std::string; using std::cin; using std::cout; using std::set; using std::queue; using std::string; using std::stringstream; using std::make_pair; using std::getline; using std::greater; using std::endl; using std::multimap; using std::deque; using std::unique; using std::lower_bound; using std::random_shuffle; using std::bitset; using std::upper_bound; using std::multiset; using std::ios; using std::make_heap; using std::push_heap; using std::pop_heap; typedef long long LL; typedef unsigned long long ULL; typedef unsigned UN; typedef pair<ULL, ULL> PAIR; typedef multimap<int, int> MMAP; typedef long double LF; const int MAXN(25); const int MAXM(310); const int MAXE(310); const int MAXK(6); const int HSIZE(131313); const int SIGMA_SIZE(26); const int MAXH(18); const int INFI((INT_MAX-1) >> 1); const ULL BASE(31); const LL LIM(1e13); const int INV(-10000); const int MOD(1000000007); const double EPS(1e-7); const LF PI(acos(-1.0)); template<typename T> inline void checkmax(T &a, T b){if(b > a) a = b;} template<typename T> inline void checkmin(T &a, T b){if(b < a) a = b;} template<typename T> inline T ABS(const T &a){return a < 0? -a: a;} int deg[MAXN]; LL dis[MAXN][MAXN]; LL dp[1 << 20]; LL solve(int n) { int can = 0; for(int i = 0; i < n; ++i) if(deg[i]&1) can |= (1 << i); if(can == 0) return 0; for(int i = 1; i <= can; ++i) { dp[i] = LIM; if((can|i) != can) continue; int mi; for(int j = 0; j < n; ++j) if(((i >> j)&1) == 1) { mi = j; break; } int ti = i^(1 << mi); for(int j = mi+1; j < n; ++j) if(((i >> j)&1) == 1) checkmin(dp[i], dp[ti^(1 << j)]+dis[mi][j]); } LL ret = LIM; for(int i = 0; i < n; ++i) if(can&(1 << i)) for(int j = i+1; j < n; ++j) if(can&(1 << j)) checkmin(ret, dp[can^(1 << i)^(1 << j)]); return ret; } int main() { int TC; scanf("%d", &TC); while(TC--) { LL ans = 0; int n, m; scanf("%d%d", &n, &m); for(int i = 0; i < n; ++i) { deg[i] = 0; for(int j = i+1; j < n; ++j) dis[i][j] = dis[j][i] = LIM; } int u, v, w; for(int i = 0; i < m; ++i) { scanf("%d%d%d", &u, &v, &w); --u, --v; ++deg[u], ++deg[v]; checkmin(dis[u][v], (LL)w); checkmin(dis[v][u], (LL)w); ans += w; } for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) for(int k = 0; k < n; ++k) checkmin(dis[j][k], dis[j][i]+dis[i][k]); bool flag(true); for(int i = 0; i < n && flag; ++i) for(int j = i+1; j < n && flag; ++j) if(dis[i][j] == LIM) flag = false; if(flag) ans += solve(n); else ans = -1; printf("%lld\n", ans); } return 0; }