注意 起点是 1 这个点
#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> #define inf 0x1f1f1f1f #define min( x,y ) ((x)>(y)?(y):(x)) using namespace std; int N,dis[12][12],dp[1<<11][11]; int main( ) { int i,j,k; while( scanf("%d",&N) && N ) { N++; memset( dis,inf,sizeof(dis) ); for( i = 1; i <= N; i++ ) dis[i][i] = 0; for( i = 1; i <= N; i++ ) for( j = 1; j <= N; j++ ) scanf("%d",&dis[i][j]); for( k = 1; k <= N; k++ ) for( i = 1; i <= N; i++ ) for( j = 1; j <= N; j++ ) { dis[i][j] = min( dis[i][j],dis[i][k] + dis[k][j] ); } memset( dp,inf,sizeof(dp) ); dp[1][1] = 0; for( i = 1; i < (1<<N); i++ ) for( j = 1; j <= N; j++ ) { if( dp[i][j] == inf ) continue; for( k = 1; k <= N; k++ ) { if( j == k ) continue; if( i&(1<<(k-1)) )continue; int stu = (i^(1<<(k-1))); dp[stu][k] = min( dp[stu][k],dp[i][j]+dis[j][k] ); } } int res = inf; for( i = 1; i <= N; i++ ) res = min( res,dp[(1<<N)-1][i] + dis[i][1] ); printf("%d\n",res); } return 0; }