http://acm.hdu.edu.cn/showproblem.php?pid=5418
/* 模板题 先用floeyed处理 */ /************************************************ * Author :Powatr * Created Time :2015-8-22 21:20:36 * File Name :B.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 1 << 17; const int inf = 0x3f3f3f3f; const int MOD = 1e9 + 7; int d[20][20]; int dp[MAXN][20]; int main(){ int T, n, m; int x, y, z; for(scanf("%d", &T); T--; ){ scanf("%d%d", &n, &m); memset(d, inf, sizeof(d)); memset(dp, inf, sizeof(dp)); for(int i = 1; i <= m; i++){ scanf("%d%d%d", &x, &y, &z); x--, y--; if(d[x][y] > z){ d[x][y] = d[y][x] = z; } } for(int i = 0; i < n; i++) d[i][i] = 0; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ for(int k = 0; k < n; k++){ d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } int maxn = (1 << n) - 1; dp[1][0] = 0; for(int i = 0; i <= maxn; i++){ for(int j = 0; j < n; j++){ for(int k = 0; k < n; k++){ if(!((i>>k)&1)) dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + d[j][k]); } } } int ans = inf; for(int i = 0 ; i < n; i++){ ans = min(ans, dp[maxn][i] + d[i][0]); } printf("%d ", ans); } return 0; }