(prim)模板题,起点可选任意一个点。
const int N = 110;
int g[N][N];
int dist[N];
bool vis[N];
int n;
int prim() {
memset(dist, 0x3f, sizeof dist);
dist[0] = 0;
int res = 0;
for(int i = 0; i < n; i++) {
int t = -1;
for(int j = 0; j < n; j++)
if(!vis[j] && (t == -1 || dist[t] > dist[j]))
t = j;
vis[t] = true;
res += dist[t];
for(int j = 0; j < n; j++)
dist[j] = min(dist[j], g[t][j]);
}
return res;
}
int main() {
cin >> n;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> g[i][j];
int t = prim();
cout << t << endl;
//system("pause");
return 0;
}