Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

C++: 
#include<stdio.h>

#define MAX 1000
#define INFINITY 10000

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

enum {
    WHITE,
    GRAY,
    BLACK
};

int prim() {
    for (int i = 0; i < n; ++i) {
        color[i] = WHITE;
        d[i] = INFINITY;
        p[i] = -1;
    }
    d[0] = 0;
    p[0] = -1;

    while (true) {
        int min = INFINITY;
        int target = -1;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min && color[i] != BLACK) {
                min = d[i];
                target = i;
            }
        }
        if (target == -1) break;

        color[target] = BLACK;

        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && M[target][v] > 0) {
                if(M[target][v] < d[v]) {
                    d[v] = M[target][v];
                    p[v] = target;
                    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() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &M[i][j]);
        }
    }
    printf("%d\n", prim());
    return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 110
#define INF 0x3f3f3f3f
int m[N][N];
int used[N];
int d[N];
int n;
int sum = 0;
void prim(){
    used[1] = 1;
    d[1] = 0;
    for(int k = 1; k < n; k++){
        int min = INF, u;
        for(int i = 1; i <= n; i++){
            if(!used[i] && d[i] < min){
                min = d[i];
                u = i;
            }
        }

        used[u] = 1;
        sum += d[u];
        for(int v = 1; v <= n; v++){
            if(!used[v] && m[u][v] < d[v]){
                d[v] = m[u][v];
            }
        }
    }
    printf("%d\n", sum);
}
int main(){

    scanf("%d", &n);
    for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) m[i][j] = INF;
    for(int i = 1; i <= n; i++){
        d[i] = INF;
        used[i] = 0;
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            int p;
            scanf("%d", &p);
            if(p != -1) m[i][j] = p;
        }
    }
    for(int i = 1; i <= n; i++) d[i] = m[1][i];
    prim();
    return 0;
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <algorithm>

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define print_array(ary, size) cout << ary[0]; rep1(i, 1, size - 1) cout << ' ' << ary[i]; cout << endl;
typedef long long ll;
typedef unsigned long long ull;

int M[100][100];
int done[100] = { 0 };

int main()
{
	int n;
	int ans = 0;
	int mincost;
	int index = 0;

	cin >> n;
	rep(i, n) {
		rep(j, n) {
			cin >> M[i][j];
		}
	}

	done[0] = 1;

	while (true) {
		mincost = -1;

		rep(i, n) {
			rep(j, n) {
				if ((done[i] == 1 && done[j] == 0)
					&& (M[i][j] != -1)
					&& (mincost == -1 || mincost > M[i][j])) {
					mincost = M[i][j];
					index = j;
				}
			}
		}

		if (mincost == -1) break;
		else {
			done[index] = 1;
			ans += mincost;
		}
	}

	cout << ans << endl;

	return 0;
}

C++: #include<iostream>
using namespace std;
#include<cstdio>
#include<queue>

class union_find  // union find.
{
    int* U;
public:
    union_find(int n){ U = new int [n]; for(int i = 0; i < n; i++) U[i] = i; }
    int root(int a){ if(U[a] == a){ return a; } U[a] = root(U[a]); return U[a]; }
    bool same(int a, int b){ return root(a) == root(b); }
    void join(int a, int b){ int x = root(a), y = root(b); U[x] = y; }
    ~union_find(){ delete [] U; }
};

struct edge{
	int v1, v2, w;
	bool operator < (const edge &e) const{ return w > e.w; }  // const?????????????????????
};

edge init(int ver_1, int ver_2, int weight)
{
	edge e;
	e.v1 = ver_1, e.v2 = ver_2, e.w = weight;
	return e;
}

int main()
{
	int i, j, w, n;
	scanf("%d", &n);

	priority_queue<edge> ES;
	queue<edge> ST;
	union_find VS(n);

	for(i = 0; i < n; i++){
		for(j = 0; j < n; j++){
			scanf("%d", &w);
			if(i >= j || w < 0) continue;
			ES.push(init(i, j, w));
		}
	}
	int size = 0;
	edge e;
	while(!ES.empty() && size < n - 1){
		e = ES.top();
		ES.pop();
		if(!VS.same(e.v1, e.v2)){
			VS.join(e.v1, e.v2);
			ST.push(e);
			size++;
		}
	};
	int sum = 0;
	while(!ST.empty()){ e = ST.front(); sum += e.w; ST.pop(); };
	printf("%d\n", sum);

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  int a[100][100],b[100]={},c[100]={};
  cin>>n;
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)cin>>a[i][j];
  c[0]=1;

  while(1){
   int k=0.min=10000,min1=0;
   if(k==n)break;
   for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
     if(a[b[i]][j]<min && a[b[i]][j]!=-1 &&c[j]!=1){
      min=a[b[i]][j];
      b[k]=j;
    }
   c[b[k]]=1
   ans+=min;
   k++;
  }
 cout<<ans<<endl;
 return 0;
}