详解:http://blog.csdn.net/wr132/article/details/43373991
http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html
#include <iostream>
#include <cstdio>using namespace std;
#define MAXV 101
#define inf 1<<29
int map[MAXV][MAXV],n;
void prim(){
int i, j, l[MAXV], visit[MAXV], r, c;//d用来记录权值之和,vis判断点是否已经加入了树,mi作为衡量最少值,v用来记录符合条件时j的值。
for (i = 1; i <= n; i++)
{
l[i] = map[1][i];
visit[i] = 0;
}
for (i = 1; i <= n; i++){
c = inf;
for ( j = 1; j <= n; j++)
{
if (!visit[j] && l[j] < c){
c = l[j];
r = j;
}
}
visit[r] = 1;
for (j = 1; j <= n; j++){
if ( !visit[j] && l[j] > map[r][j])
{
l[j] = map[r][j];
}
}
}
for(l[0]=0,i=1;i<=n;i++) l[0]+=l[i];
printf("%d\n",l[0]);
}
int main(){
int i,j;
while(scanf("%d",&n),n){
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
prim();
}
return 0;
}