#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
int n=5;
int map[5][5]={
0,17,2,9,pow(2,31)-1,
17,0,pow(2,31)-1,5,20,
2,pow(2,31)-1,0,13,12,
9,5,13,0,8,
pow(2,31)-1,20,12,8,0
};
int known[n];
int dist[n];
int parent[n];
for(int i=0;i<n;i++){
known[i]=-1;
if(i==0){
dist[i]=0;
}else{
dist[i]=pow(2,31)-1;
}
parent[i]=-1;
}
for(int i=0;i<n;i++){
int m;
int min=pow(2,31)-1;
for(int j=0;j<n;j++){
if(known[j]==-1&&dist[j]<min){
m=j;
min=dist[j];
}
}
known[m]=1;
for(int j=0;j<n;j++){
if(known[j]==-1&&map[m][j]<dist[j]){
dist[j]=map[m][j];
parent[j]=m;
}
}
}
int sum=0;
printf("该最小生成树的路径有:\n");
for(int i=0;i<n;i++){
if(parent[i]!=-1){
printf("V%d->V%d,长度为%d\n",i,parent[i],dist[i]);
}
sum+=dist[i];
}
printf("树的总权值为%d",sum);
return 0;
}