Go: package main

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

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <iostream>
#include <cstdio>
#define MAX_VERTEXES 100
#define NIL -1
#define ZERO 0
#define INFTY 2001//>2000
using namespace std;

//think easy make easy


int n;//num of vertexes(頂点)
int matrix_graph[MAX_VERTEXES+1+1][MAX_VERTEXES+1+1];//1オリジン
bool vertix_set[MAX_VERTEXES+1+1];
int mincst = 0;

void findMinEdge(){//次に加える辺を決定する関数
	int minval_tmp = INFTY;
	int next_vertex;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(vertix_set[i] == true && vertix_set[j] == false){
				if(matrix_graph[i][j] < minval_tmp && matrix_graph[i][j] != NIL){
					next_vertex = j;
					minval_tmp = matrix_graph[i][j];
				}
			}
		}
	}

	mincst += minval_tmp;
	vertix_set[next_vertex] = true;
}

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


	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			matrix_graph[i][j] = ZERO;
		}
	}

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

	/*確認用
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(j<n) printf("%d ",matrix_graph[i][j]);
			else printf("%d\n",matrix_graph[i][j]);
		}
	}
	*/

	int minval_tmp = INFTY;
	int next_vertex1;
	int next_vertex2;

	for(int i=1;i<=n;i++){
		vertix_set[i] = false;
	}

	for(int i=1;i<=n;i++){//しつこい
		for(int j=1;j<=n;j++){
			if(matrix_graph[i][j] < minval_tmp && matrix_graph[i][j] != NIL){
				minval_tmp = matrix_graph[i][j];
				next_vertex1 = i;
				next_vertex2 = j;
			}
		}
	}
	vertix_set[next_vertex1] = true;
	vertix_set[next_vertex2] = true;
	mincst += minval_tmp;

	

	for(int i=1;i<n-1;i++){
		findMinEdge();
	}
	cout <<mincst <<endl;

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <algorithm>
#include <deque>
using namespace std;

#define INF 1e18
#define int long long

signed main() {
    int n; cin >> n;
    vector<vector<int> > adj(n, vector<int> (n));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            int a; cin >> a;
            adj[i][j] = (a == -1 ? INF : a);
        }
    }

    int u , minv;
    int unvisited = 0; int visited = 1; int fixed = 2;
    vector<int> dist(n, INF), parent(n, -1), status(n, unvisited);

    dist[0] = 0;

    while(1) {
        u = -1;
        minv = INF;
        for(int i = 0; i < n; i++) {
            if(minv > dist[i] && status[i] != fixed) {
                u = i;
                minv = dist[i];
            }
        }
        if(u == -1) break;
        status[u] = fixed;
        for(int v = 0; v < n; v++) {
            if(status[v] != fixed && adj[u][v] != INF) {
                if(dist[v] > adj[u][v]) {
                    dist[v] = adj[u][v];
                    parent[v] = u;
                    status[v] = visited;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0; i < n; i++) {
        sum += dist[i];
    }

    cout << sum << endl;
    return 0;
}

C++: #include <iostream>
#include <limits>
#define N 110

int main(){
  int n;
  std::cin >> n;
 
  int A[N][N];
  int d[N] = {};
  int p[N];
  for(int i=0; i<n; i++){
    d[i] = std::numeric_limits<int>::max();
    p[i] = -1;
    for(int j=0; j<n; j++){
      std::cin >> A[i][j];
      if( A[i][j] == -1){ //no line
        A[i][j] = std::numeric_limits<int>::max();
      }
    }
  }
  d[0] = 0;

  int used[N] = {};
  while(1){
    int u = -1;
    int min = std::numeric_limits<int>::max();
    for(int i=0; i<n; i++){
      if( used[i] != -1 && d[i]<min ){
        min = d[i];
        u = i;
      }
    }
    if(min==std::numeric_limits<int>::max()) break;
    used[u] = -1;

    for(int i=0; i<n; i++){
      if( used[i] != -1 && A[u][i] < d[i]){
        p[i] = u;
        d[i] = A[u][i];
        used[i] = 1;
      }
    }
  }
  int MST=0;
  for(int i=0; i<n; i++){
    if(p[i] != -1) MST += A[i][p[i]];
      }

  std::cout << MST << std::endl;

  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;

int root(int i,vector<int> &parent){
    if(parent[i]==-1){
        //親がいない時
        return i;
    }else{
        int result=root(parent[i],parent);
        parent[i]=result; //パス圧縮
        return result;
    }
}

void unite(int x,int y,vector<int> &parent){
    if(root(x,parent)!=root(y,parent)){
        parent[root(y,parent)]=root(x,parent);
    }
}

bool issame(int x,int y,vector<int> &parent){
    return root(x,parent)==root(y,parent);
}

int main(){
    int n;
    cin >> n;
    //first:cost,second:(first:from,second:to)
    vector<pair<int,pair<int,int> > > bridges(n*n);

    //入力
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            int cost;
            if(cin >> cost,cost!=-1){
                bridges.push_back(pair<int,pair<int,int> >(cost,pair<int,int>(i,j)));
            }
        }
    }

    //sort
    sort(bridges.begin(),bridges.end());

    //島を管理する用
    vector<int> parents(n*n,-1);

    int sum=0;

    for(int i=0;i<bridges.size();++i){
        if(!issame(bridges[i].second.first,bridges[i].second.second,parents)){
            unite(bridges[i].second.first,bridges[i].second.second,parents);
            sum+=bridges[i].first;
        }
    }
    cout << sum;
}