Floyd模板题。
//输入顶点从1始,内部运行顶点从1始
//其实顶点从0编号也可以,改一改就好了
#include <iostream>
#include <vector>
#define MAXSIZE 100
#define INF 0x7fffffff
using namespace std;
int main(){
int t,n,m;//vexes' num,edge's num,sourse vex,destination vex
int thisNode,nextNode;
int newDist;
int dist[MAXSIZE+1][MAXSIZE+1];
int totalDist[MAXSIZE+1];
cin>>t;
while (t--){
cin>>n>>m;
//initiate
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
if (i==j)
dist[i][j]=0;
else
dist[i][j]=INF;
}
totalDist[i]=0;
}
//input
for (int i=0;i<m;i++){
cin>>thisNode>>nextNode;
dist[thisNode][nextNode]=dist[nextNode][thisNode]=1;//适用于无向图
}
//floyd
for (int k=1;k<=n;k++){
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
if (i==k||j==k)
continue;//第k趟邻接矩阵十字区不改变
if (dist[i][k]==INF||dist[k][j]==INF)//经过k点不可达
newDist=INF;
else
newDist=dist[i][k]+dist[k][j];//经过k点可达
if (newDist<dist[i][j])//比较新旧距离
dist[i][j]=newDist;
}
}
}
//cal
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
if (dist[i][j]==INF)
totalDist[i]+=n;
else
totalDist[i]+=dist[i][j];
}
}
int minVex=1;
for (int i=1;i<=n;i++){
if (totalDist[i]<totalDist[minVex])
minVex=i;
}
//output
cout<<minVex<<endl;
}
return true;
}