貌似就是个裸的最小生成树啊
*******************************************************************************
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 105;
const int oo = 0xfffffff;
int G[maxn][maxn];
int prim(int N)
{
int i, j, dist[maxn]={0}, use[maxn]={0, 1};
for(i=2; i<=N; i++)
dist[i] = G[1][i];
for(i=1; i<N; i++)
{
int k = 1, Min = oo;
for(j=1; j<=N; j++)
{
if(!use[j] && Min > dist[j])
Min = dist[j], k = j;
}
use[k] = true;
for(j=1; j<=N; j++)
if(!use[j])dist[j] = min(dist[j], G[k][j]);
}
int ans = 0;
for(i=1; i<=N; i++)
ans += dist[i];
return ans;
}
int main()
{
int N;
while(scanf("%d", &N) != EOF)
{
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
{
scanf("%d", &G[i][j]);
}
int ans = prim(N);
printf("%d\n", ans);
}
return 0;
}