#define MAXN 101
int n, ret;
int map[MAXN][MAXN];
void prim()
{
int closet[MAXN];
int dist[MAXN];
int i, j;
for ( i=0; i<n; i++)
{
closet[i] = 0;
dist[i] = map[0][i];
}
closet[0] = 1;
int u, min;
for ( i=1; i<n; i++)
{
min = 100001;
for ( j=1; j<n; j++)
{
if ( ! closet[j] && dist[j] < min && dist[j] > 0)
{
u = j;
min = dist[j];
}
}
closet[u] = 1;
ret += min;
for ( j=1; j<n; j++)
{
if (! closet[j] && map[u][j] < dist[j] )
dist[j] = map[u][j];
}
}
}
#include <stdio.h>
#include <queue>
using namespace std;
#define Maxv 100+5
typedef struct
{
int v1,v2,len;
}Node;
bool operator > (Node a,Node b)
{
if( a.len > b.len )
return true;
return false;
}
int dis[Maxv][Maxv];
int fa[Maxv];
int Getfa(int i)
{
if( fa[i] == i ) return i;
fa[i] = Getfa(fa[i]);
return fa[i];
}
int main()
{
int sum;
priority_queue< Node,vector<Node>,greater<Node> > Q; //返回最小数
int vn,i,j;
while( scanf( "%d" , &vn ) != EOF )
{
//输入图部分
for( i = 1 ; i <= vn ; i++ )
for( j = 1 ; j <= vn ; j++ )
scanf( "%d" , &dis[i][j] );
for( i = 1 ; i <= vn ; i++ ) fa[i] = i;
while( !Q.empty() ) Q.pop();
//把每条边压入堆中
for( i = 1 ; i < vn ; i++ )
for( j = i+1 ; j <= vn ; j++ )
if( dis[i][j] )
{
Node e;
e.v1 = i , e.v2 = j , e.len = dis[i][j];
Q.push(e);
}
sum = 0;
while( Q.size() != 0 )
{
Node e;
e = Q.top();
Q.pop();
if( Getfa(e.v1) != Getfa(e.v2) )
{
sum += e.len;
fa[Getfa(e.v2)] = Getfa(e.v1);
}
}
printf( "%d\n" , sum ); //所需的最小值
}
return 0;
}
#define MAXN 101int n, ret;int map[MAXN][MAXN];void prim(){ int closet[MAXN]; int dist[MAXN]; int i, j; for ( i=0; i<n; i++) { closet[i] = 0; dist[i] = map[0][i]; } closet[0] = 1; in