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
	for len(edges) > 0 {
		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
	}
	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>
using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

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]=WHITE;
    }

    d[0]=0;

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

int main(){
    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;
}

C++: //
// Created by 李青坪 on 2018/11/6.
//

#include <cstdio>
#include <algorithm>
#include <set>
#define MAXN 100
#define INFINITY 1<<20
#define NIL -1
using namespace std;

int n;
int sum;
int graph[MAXN][MAXN];
int d[MAXN];
int parent[MAXN];
set<int> T;
set<int> V_T;

void init(){
    fill(d, d+n, INFINITY);
    fill(parent, parent+n, NIL);
    T.insert(0);
    for (int i = 1; i < n; ++i) {
        V_T.insert(i);
    }
    for (int j = 0; j < n; ++j) {
        d[j] = graph[0][j];
    }
}

void updateMinWeight(){

}

pair<int, pair<int, int> > findMinEdge(){
    int min = INFINITY;
    pair<int, int> r;
    set<int >::iterator itr;
    for (itr = T.begin(); itr != T.end(); itr++){
        for (set<int>::iterator itr1 = V_T.begin(); itr1 != V_T.end(); itr1++){
            if (graph[*itr][*itr1] < min){
                min = graph[*itr][*itr1];
                r = make_pair(*itr, *itr1);
            }
        }
    }
    return make_pair(min, r);
}

void findMinSpanTree(){
    while (!V_T.empty()){
        pair<int, pair<int, int> > v = findMinEdge();
        sum += v.first;
        parent[v.second.second] = v.second.first;
        T.insert(v.second.second);
        V_T.erase(V_T.find(v.second.second));
    }
}

int main(){
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        int w;
        for (int j = 0; j < n; ++j) {
            scanf("%d", &w);
            if (w == -1) graph[i][j] = INFINITY;
            else graph[i][j] = w;
        }
    }
    init();
    findMinSpanTree();
    printf("%d\n", sum);
}
/*
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<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF (1<<21)
#define white 0
#define gray 1
#define black 2

int n;
int W[100][100];
int color[100],d[100],p[100];

int prim() {

  d[0] = 0;

  while(true) {
    int mini = INF,u;
    for(int i = 0;i < n;i++) {
      if(d[i] < mini && color[i] != black) {
        mini = d[i];
        u = i;
      }
    }

    

    if(mini == INF) break;
    color[u] = black;

    for(int i = 0;i < n;i++) {
      if(color[i] != black && W[u][i] != -1) {
        if(W[u][i] < d[i]) {
          d[i] = W[u][i];
          p[i] = u;
        }
      }
    }
  }

  int sum = 0;
  for(int i = 0;i < n;i++) {
    if(p[i] != -1) sum += W[i][p[i]];
  }
 
  return sum;

}


int main() {

  cin >> n;

  for(int i = 0;i < n;i++) {
    color[i] = white;
    d[i] = INF;
    p[i] = -1;
    for(int j = 0;j < n;j++) {
      int a;
      cin >> a;
      W[i][j] = a;
    }
  }

 
    cout << prim() << endl;

}

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 105, INF = 10000;
const int WHITE = 0, GRAY = 1, BLACK = 2;

int n;
int M[MAX_N][MAX_N], color[MAX_N], d[MAX_N], p[MAX_N];

void init(){
    rep(i,MAX_N){
        rep(j,MAX_N) M[i][j] = -1;
        color[i] = WHITE;
        d[i] = INF;
    }
}

void prim(int u){
    d[u] = 0;
    p[u] = -1;

    while(true){
        int mincost = INF;
        rep(i,n){
            if(color[i] != BLACK && mincost > d[i]){
                mincost = d[i];
                u = i;
            }
        }
        if(mincost == INF) break;
        color[u] = BLACK;
        rep(v,n){
            if(color[v] != BLACK && M[u][v] != -1){
                if(M[u][v] < d[v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
}

int main()
{
    cin >> n;
    init();
    rep(i,n){
        rep(j,n){
            int aij; cin >> aij;
            if(aij != -1) M[i][j] = aij;
        }
    }
    prim(0);
    int sum = 0;
    rep(i,n) sum += d[i];
    cout << sum << endl;
}