Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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

class union_find  // union find.
{
	int* U;
public:
	union_find(int n){ U = new int [n + 1]; for(int i = 1; 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; int weight; };  // edge.

edge init(){
	edge e;
	e.v1 = 0; e.v2 = 0; e.weight = -1;
	return e;
}

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

void swap_e(edge e1, edge e2)
{
	swap(e1.v1, e2.v1); swap(e1.v2, e2.v2); swap(e1.weight, e2.weight);
}

class edge_heap  // heap.
{
	edge* E;
	int edge_n;
public:
	edge_heap(int n){ E = new edge [n + 1]; edge_n = 0; }
	void min_heap(int i);
	void insert_heap(edge e);   // sorting.
	void insert_nl(edge e);     // no sorting.
	edge delete_min();
	bool isEmpty(){ return edge_n == 0; }
	int total_weight();
	~edge_heap(){ delete [] E; }
};

void edge_heap::min_heap(int i)
{
	int l = 2 * i, r = 2 * i + 1;
	if(l > edge_n && r > edge_n) return;
	int minid = i;
	if(l <= edge_n){ if(E[l].weight < E[minid].weight) minid = l; }
	if(r <= edge_n){ if(E[r].weight < E[minid].weight) minid = r; }
	if(minid == i) return;
	swap(E[minid], E[i]);
	min_heap(minid);
}

void edge_heap::insert_heap(edge e)
{
	edge_n++;
	int w = e.weight;
	int i = edge_n;
	while(i > 1){
		if(w < E[i / 2].weight){
		E[i] = E[i / 2];
		}else{ break; }
		i /= 2;
	};
	E[i] = e;
}
void edge_heap::insert_nl(edge e){ edge_n++; E[edge_n] = e; }

edge edge_heap::delete_min()
{
	edge e = E[1];
	E[1] = E[edge_n]; edge_n--;
	min_heap(1);
	return e;
}

int edge_heap::total_weight()
{
	int tw = 0;
	for(int i = 1; i <= edge_n; i++) tw += E[i].weight;
	return tw;
}

int main()
{
	int n, i, j, w;
	cin >> n;
	edge_heap EH(n * (n + 1) / 2); // All Edge heap.
	edge e;

	for(i = 1; i <= n; i++){
		for(j = 1; j <= n; j++){
			cin >> w;
			if(j <= i || w < 0) continue;
			e = init(i, j, w);
			EH.insert_heap(e);
		}
	}

	edge_heap TH(n);   // Tree heap.

	union_find VG(n);  // vertex group.

	while(!EH.isEmpty()){
		e = EH.delete_min();
		if(!VG.same(e.v1, e.v2)){
			VG.join(e.v1, e.v2);
			TH.insert_nl(e);
		};
	};

	cout << TH.total_weight() << endl;

	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i) 
#define PI acos(-1)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

struct DisjointSet {
private:
    vector<int> par;
    vector<int> rank;

public:
    DisjointSet(int n) {
        par = vector<int>(n);
        rep(i, n) par[i] = i;
        rank = vector<int>(n);
    }

    int find(int i) {
        if (par[i] == i) return i;
        int p = find(par[i]);
        par[i] = p;
        return p;
    }

    bool joint(int i, int j) {
        int pi = find(i);
        int pj = find(j);
        if (pi == pj) return false;

        if (rank[pi] < rank[pj]) this->par[pi] = pj;
        else if (rank[pj] < rank[pi]) this->par[pj] = pi;
        else {
            this->par[pi] = pj;
            rank[pj]++;
        }
        return true;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin>>n;
    priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
    rep(i, n) {
        rep(j, n) {
            int w;
            cin>>w;
            if (w == -1) continue;

            vector<int> v = {w, i, j};
            q.push(v);
        }
    }
    DisjointSet ds(n);
    ll ans = 0;
    while (!q.empty()) {
        vector<int> v = q.top(); q.pop();
        int w = v[0];
        int v1 = v[1];
        int v2 = v[2];
        if (ds.joint(v1, v2)) ans += w;
    }
    cout<<ans<<endl;
}

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

#define INF 2000

int n;
int cost[110][110], mincost[110];
bool used[110];

int prim(){

  //initialization
  for(int i = 0;i < n;i++){
    mincost[i] = INF;
    used[i] = false;
  }

  //cost of starting position is 0
  mincost[0] = 0;
  int ans = 0;
  
  while(true){
    int v = -1;
    
    for(int u = 0;u < n;u++){
      if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
    }

    if(v == -1) break; //finish
    used[v] = true; //use v
    ans += mincost[v];

    //update mincost
    for(int u = 0;u < n;u++){
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  
  return ans;
}

int main(){

  //input
  cin >> n;
  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      cin >> cost[i][j];
      if(cost[i][j] == -1) cost[i][j] = INF;
    }
  }

  //output
  cout << prim() << endl;
  
  return 0;
}

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

const int MAX = 100;

struct Edge {
	Edge(int v1, int v2, int cost) :v1(v1), v2(v2), cost(cost) {};
	bool operator < (const Edge& e) const {
		return cost > e.cost;
	}
	int v1;
	int v2;
	int cost;
};

int parent(int uft[], int c) {
	if (uft[c] == -1)return c;
	return uft[c] = parent(uft, uft[c]);
}

int main() {
	int n;
	int uft[MAX];
	int sum = 0;
	priority_queue<Edge> q;
	
	cin >> n;

	for (int i = 0; i < n; i++) {
		uft[i] = -1;
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int temp;
			cin >> temp;
			if (temp != -1){
				Edge e(i, j, temp);
				q.push(e);
			}
		}
	}

	while (!q.empty()) {
		Edge e = q.top(); q.pop();
		int p1 = parent(uft, e.v1);
		int p2 = parent(uft, e.v2);

		if (p1 != p2) {
			uft[p1] = p2;
			sum += e.cost;
		}
	}

	cout << sum << endl;
}

C++: #include<iostream>
#define MAX 101
#define INF (2<<29)
using namespace std;
int main(){
  int n;
  cin>>n;
  int graph[MAX][MAX];
  
  for(int i=1;i<n+1;i++){
    for(int j=1;j<n+1;j++){
      cin>>graph[i][j];
      if(graph[i][j]==-1)graph[i][j]=INF;
    }
  }
  
  int visited[MAX];
  int min[MAX];
  for(int i=1;i<n+1;i++){
    visited[i]=0;
    min[i]=INF;
  }
  
  min[1]=0;
  int ans=0;
  while(true){
    int a=-1;
    for(int i=1;i<n+1;i++){
      if(visited[i]==0&&(a==-1||min[i]<min[a]))a=i;
    }

    if(a==-1)break;
    visited[a]=1;
    ans +=min[a];

    for(int i=1;i<n+1;i++){
      min[i]=min[i]<graph[a][i] ? min[i]:graph[a][i];
    }
  }
  cout<<ans<<endl;
  return 0;
}