#include<iostream>
using namespace std;
#include<stdio.h>
const int maxn=100;
bool s[maxn];
int lowcost[maxn];
int closest[maxn];
int c[maxn][maxn];
void prim(int n)
{
int total=0;
s[1]=true;
for(int i=2;i<=n;i++)//首先对三个数组进行初始化
{
lowcost[i]=c[1][i];
closest[i]=1;
s[i]=false;
}
printf("the MST is :\n");
for(int i=1;i<=n-1;i++)//然后的工作就是添加n-1条边
{
int minn=10000;
int j=1;
for(int k=2;k<=n;k++)//选出最小的边
{
if(minn>lowcost[k]&&!s[k])
{
minn=lowcost[k];
j=k;
}
}
printf("%d %d %d\n",j,closest[j],lowcost[j]);
total+=lowcost[j];
s[j]=true;
for(int k=2;k<=n;k++)//更新lowest和closest数组
{
if((c[j][k]<lowcost[k])&&!s[k])
{
lowcost[k]=c[j][k];
closest[k]=j;
}
}
}
printf("the total price is:%d\n",total);
}
int main()
{
int n;
scanf("%d",&n);//输入n的值
for(int i=1;i<=n;i++)//就是对数组c进行更新
for(int j=i;j<=n;j++)
{
int temp;
scanf("%d",&temp);
c[i][j]=temp;
c[j][i]=temp;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
printf("%d ",c[i][j]);
if(j==n)
printf("\n");
}
prim(n);
return 0;
}