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<bits/stdc++.h>
using namespace std;
const int MAX=101;
const int INF=123456789;


class union_find{
public:
  vector<int> rank,pare;
  union_find(){}
  union_find(int size){
    rank.resize(size,0);
    pare.resize(size,0);
    for(int i=0;i<size;i++){
      init(i);
    }
  }

  void init(int x){
    pare[x]=x;
    rank[x]=0;
  }

  int findpare(int x){
    if(pare[x]==x){
      return x;
    }
    else{
      return pare[x]=findpare(pare[x]);
    }
  }

  bool issame(int x,int y){
    return findpare(x)==findpare(y);
  }

  void unite(int x,int y){
    x=findpare(x);
    y=findpare(y);

    if(x==y) return;
    if(rank[x]<rank[y]){
      pare[x]=y;
    }
    else{
      pare[y]=x;
      if(rank[x]==rank[y]) rank[x]++;
    }
  }


};

int n,E;
struct edge{
  int u,v,cost;
};

edge es[MAX];


bool comp(const edge& e1,const edge& e2){
  return e1.cost<e2.cost;
}


int kruskal(){
  sort(es,es+E,comp);//E???????????°
  int res=0;
  union_find uf=union_find(n);
  for(int i=0;i<E;i++){
    edge e=es[i];
    if(!uf.issame(e.u,e.v)){
      uf.unite(e.u,e.v);
      res+=e.cost;
    }
  }
  return res;
}

int main(){
  cin>>n>>E;
  for(int i=0;i<E;i++){
    int s,t,w;
    cin>>s>>t>>w;
    es[i].u=s;
    es[i].v=t;
    es[i].cost=w;
  }

  cout<<kruskal()<<endl;
  return 0;
}

C++: import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

@SuppressWarnings("unchecked")
class MinCostTree{

    int N;
    class Edge{
        int from,to;
        long w;
        Edge(int from,int to,long w){this.from=from;this.to=to;this.w=w;}
    }
    List<Edge> edges[];
    List<Edge> minedge[];
    void add_edge(int u,int v,long w){
        edges[u].add(new Edge(u,v,w));
        edges[v].add(new Edge(v,u,w));
    }
    MinCostTree(int N){
        this.N=N;
        edges = new ArrayList[N];
        minedge = new ArrayList[N];
        for(int i=0;i<N;++i)edges[i]=new ArrayList<>();
        for(int i=0;i<N;++i)minedge[i]=new ArrayList<>();
    }
    long mincost(){
        PriorityQueue<Edge> que = new PriorityQueue<>((a,b)->a.w<b.w ? -1:1);
        boolean used[] = new boolean[N];
        used[0]=true;
        long res = 0;
        for(Edge e : edges[0])que.add(e);
        while(!que.isEmpty()){
            Edge e = que.poll();
            if(used[e.to])continue;
            used[e.to]=true;
            minedge[e.from].add(e);
            res += e.w;
            for(Edge ed : edges[e.to])que.add(ed);
        }
        return res;
    }
}

class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        MinCostTree mct = new MinCostTree(n);
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                long a = scan.nextLong();
                if(i>j && a>0)mct.add_edge(i, j, a);
            }
        }
        System.out.println(mct.mincost());
    }
}

C++: #include <iostream>
using namespace std;
#define INF 1<<21
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX 100

int v_num;
int adjM[MAX+1][MAX+1];

int PrimsAlgorithm() 
{
    int current_vertex;
    int color[v_num+1], min_edge[v_num+1], parent[v_num+1];

    // set init state
    for (int i = 0; i < v_num; ++i)
    {
        color[i] = WHITE;
        min_edge[i] = INF;
        parent[i] = -1;
    }

    min_edge[0] = 0;

    while (true) 
    {
        int min_cost = INF;
        // move to the minimum vertex which can be accessed 
        for (int i = 0; i < v_num; ++i) 
        {
            if (color[i] != BLACK && min_edge[i] < min_cost)
            {
                min_cost = min_edge[i];
                current_vertex = i;
            }
        }

        if (min_cost == INF) break;

        color[current_vertex] = BLACK;

        for (int i = 0; i < v_num; ++i) 
        {
            if (color[i] != BLACK && adjM[current_vertex][i] != -1)
            {
                if (adjM[current_vertex][i] < min_edge[i])
                {
                    min_edge[i] = adjM[current_vertex][i];
                    parent[i] = current_vertex;
                    color[i] = GRAY;
                }
            } 
        }
    }

    int sum = 0;
    for (int i = 0; i < v_num; ++i) 
    {
        if (parent[i] != -1) sum += adjM[i][parent[i]];
    }
    return sum;
}

int main() 
{
    int v;
    cin >> v_num;
    for (int i = 0; i < v_num; ++i) 
    {
        for (int j = 0; j < v_num; ++j)
        {
            cin >> v;
            adjM[i][j] = v != -1 ? v : INF;
        }
    }
    
    cout << PrimsAlgorithm() << endl;
}