#include <iostream> using namespace std; int dis[32][32]; int dp[32][32][32]; #define MAX 0x01010101 //三维动态规划: 本题采用递推的方式 //设我们假设某一时刻三辆车分别在i,j,k三个位置. 不妨采取递推的方式继续下去: //到第k点的可能状态有: // dp[i][j][k]+dis[i][k+1](dp[j][k][k+1], dp[k][j][k+1]) // dp[i][j][k]+dis[j][k+1](dp[i][k][k+1], dp[k][i][k+1]) // dp[i][j][k]+dis[k][k+1](dp[i][j][k+1], dp[j][i][k+1]) //其中dp[i][j][k]表示三辆车分别在i,j,k位置时的最小时间(也就是到k). int main() { int N; scanf("%d", &N); while(N--) { //input int n; scanf("%d", &n); for(int i = 1; i <= n-1; ++i) for(int j = i+1; j <= n; ++j) { scanf("%d", &dis[i][j]); dis[j][i] = dis[i][j]; } //dp memset(dp, 0x01, sizeof(dp)); dp[1][1][1] = 0; for(int k = 1; k <= n-1; ++k) for(int i = 1; i <= k; ++i) for(int j = 1; j <= k; ++j) if(dp[i][j][k] != MAX) { if(dp[j][k][k+1] > dp[i][j][k]+dis[i][k+1]) { dp[j][k][k+1] = dp[i][j][k]+dis[i][k+1]; dp[k][j][k+1] = dp[i][j][k]+dis[i][k+1]; } if(dp[i][k][k+1] > dp[i][j][k]+dis[j][k+1]) { dp[i][k][k+1] = dp[i][j][k]+dis[j][k+1]; dp[k][i][k+1] = dp[i][j][k]+dis[j][k+1]; } if(dp[i][j][k+1] > dp[i][j][k]+dis[k][k+1]) { dp[i][j][k+1] = dp[i][j][k]+dis[k][k+1]; dp[j][i][k+1] = dp[i][j][k]+dis[k][k+1]; } } // find min int min = MAX; for(int i = 1; i <= n-1; ++i) for(int j = 1; j <= n-1; ++j) if(dp[i][j][n] < min) min = dp[i][j][n]; printf("%d/n", min); } return 0; }