Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] >= 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A >= edges[j].A })

	sum := 0
	num := 0
	for len(edges) > 0 && num < n-1 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
		num += 1
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <climits>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repR(i, n) for(int i = (n) - 1; i > -1; i--)
#define rep1(i, n) for(int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for(int i = (n); i > 0; i--)
using namespace std;
static const int INF = INT_MAX;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

static const int N = 2010;
int M[N][N];
int color[N];
int d[N];
int p[N];
int n;

void prim() {
  rep(u, n) {
    color[u] = WHITE;
    d[u] = INF;
  }
  d[0] = 0;
  p[0] = -1;

  while (true) {
    int mincost = INF;
    int u;
    rep(i, n) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INF) break;

    color[u] = BLACK;

    rep(v, n) {
      if (color[v] == BLACK || M[u][v] == INF) continue;
      if (M[u][v] < d[v]) {
        d[v] = M[u][v];
        p[v] = u;
        color[v] = GRAY;
      }
    }
  }
}

int main() {
  cin >> n;
  rep(y, n) rep(x, n) {
    int a;
    cin >> a;
    if (a != -1) M[y][x] = a;
    else M[y][x] = INF;
  }
  prim();
  cout << accumulate(d, d + n, 0) << endl;
  return 0;
}

C++: #include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);

int n, M[MAX][MAX];

int prim()
{
    int u, minv;
    int d[MAX], p[MAX], color[MAX];

    for(int i=0; i<n; i++)
    {
        d[i] = INFTY;
        p[i] = -1;
        color[i] = 0;
    }
    d[0] = 0;
    
    while(true)
    {
        minv = INFTY;
        u = -1;
        for(int i=0; i<n; i++)
        {
            if(minv > d[i] && color[i] != 2)
            {
                u = i;
                minv = d[i];
            }
        }
        if(u == -1) break;
        color[u] = 2;
        for(int v=0; v<n; v++)
        {
            if(color[v] != 2 && M[u][v] != INFTY)
            {
                if(d[v] > M[u][v])
                {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = 1;
                }
            }
        }
    }
    int sum = 0;
    for(int i=0; i<n; i++)
    {
        if(p[i] != -1) sum += M[i][p[i]];
    }
    return sum;
}

int main() 
{
    ios::sync_with_stdio(false);
    
    cin >> n;
    
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            int e;
            cin >> e;
            M[i][j] = (e == -1) ? INFTY : e;
        }
    }
    
    cout << prim() << endl;

    return 0;
}

C++: #include <iostream>

using namespace std;
int n;
int dat[100][100];
int cost[100];
int done[100];

void prim(){
	for(int i=0;i<n;i++){
		cost[i]=1<<21;
		done[i]=0;
	}
	int s=0;	
	int next, ncost=1<<21;
	done[s]=1;
	cost[s]=0;
	int cnt=0;
	while(1){
		cnt++;
		if(cnt>20)
			return;
		
		ncost=1<<21;
		for(int j=0;j<n;j++){
			if(done[j]){
				for(int i=0;i<n;i++){
					if(dat[j][i]!=-1){
						if(cost[i]>=dat[j][i]){
							cost[i]=dat[j][i];
						}
						if(ncost>cost[i]&&!done[i]){
							next=i;
							ncost=cost[i];
						}
					}
				}
			}
		}
//		cout<<"next:"<<next<<" cost:"<<cost[next]<<endl;
		done[next]=1;
		int flag=true;
		for(int i=0;i<n;i++){
			if(!done[i]) flag = false;
		}
		if(flag)
			return;
	}
}

int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>dat[i][j];
		}
	}
	prim();
	int sum=0;
	for(int i=0;i<n;i++){
		sum+=cost[i];
	}
	cout<<sum<<endl;
	return 0;
}

C++: 
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e2+5;
const int inf=(1<<21);
const int white=0;
const int gray=1;
const int black=2;

int n,m[maxn][maxn];

int prim()
{
    int u,minv,sum=0;
    int d[maxn],p[maxn],color[maxn];
    for(int i=0;i<n;i++){
        d[i]=inf;
        color[maxn]=white;
        p[i]=-1;
    }
    d[0]=0;
    while(1){
        minv=inf;
        u=-1;
        for(int i=0;i<n;i++){
            if(minv<d[i]&&color[i]==white){
                minv=d[i];
                u=i;
            }
        }
        if(u==-1){
            break;
        }
        color[u]=black;
        sum+=d[u];
        for(int i=0;i<n;i++){
            if(color[i]!=black&&d[i]<m[u][i]){
                d[i]=m[u][i];
                p[i]=u;
                color[i]=gray;
            }
        }
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int e;
            cin>>e;
            m[i][j]=(e==-1)?inf:e;
        }
    }
    cout<<prim()<<endl;
    return 0;
}
/*
5
-1 2 3 1 -1
2 -1 -1 4 -1
3 -1 -1 1 1
1 4 1 -1 3
-1 -1 1 3 -1
*/

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int P[1010];
int cost[5000];
int src[5000];	
int dst[5000];
int A[100 + 10][100 + 10];
int M = 0;
vector<pair<int,int>> edges;

void init(int N)
{
    for(int i = 1; i <= N; ++i)
    {
        P[i] = i;
    }
}
 
int root(int a)
{
    if(P[a] == a)
    {
        return a;
    }
    return (P[a] = root(P[a]));
}
 
bool is_same_set(int a, int b)
{
    return root(a) == root(b);
}
 
void unite(int a, int b)
{
    P[root(a)] = root(b);
}

int main()
{
	int n = 0;
	cin >> n; 
	for(int r = 0; r < n; ++r)
	{
		for(int c = 0; c < n; ++c)
		{
			cin >> A[r][c];
		}
	}
	for (int r = 0; r < n; ++r)
	{
		for(int c = r + 1; c < n; ++c)
		{
			if(A[r][c] > -1)
			{
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				edges.push_back(make_pair(cost[M],M));
				M += 1;
			}
		}
	}
	sort(edges.begin(),edges.end());
	int sum = 0;
	init(n);
	for(int i = 0; i < M; ++i)
	{
		if(is_same_set(src[edges[i].second], dst[edges[i].second]) == 1) continue;
			unite(dst[edges[i].second],src[edges[i].second]);
			sum += edges[i].first;
	}
	cout << sum << endl;
}