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;
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, A[MAX][MAX];

int prim(){
    int u, minv;
    int d[MAX], p[MAX], color[MAX];

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

    while(1){
        minv = INFTY;
        for(int i = 0; i < n; i++){
            if(color[i] != BLACK && d[i] < minv){
                minv = d[i];
                u = i;
            }
        }

        if(minv == INFTY) break;

        color[u] = BLACK;

        for(int v = 0; v < n; v++){
            if(color[v] != BLACK && A[u][v] != INFTY){
                if(A[u][v] < d[v]){
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }

    int sum = 0;
    for(int i = 0; i < n; i++){
        sum += d[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;
            A[i][j] = (e == -1) ? INFTY : e;
        }
    }

    cout << prim() << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
#define eps 1e-7
//#define Kongxiangzhouye

static const int MAX=100;
static const int INF=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int begin[10000];
int to[10000];
int next[10000];
int weight[10000];
int e,n;
void add(int x,int y,int z){
	to[++e]=y;
	next[e]=begin[x];
	begin[x]=e;
	weight[e]=z;
}

struct p{
	int edge;
	int weight;
	bool operator < (const p& u)const{
		return weight>u.weight;
	}
};
int createtree(){
	priority_queue<p> q;
	int flag[100];
	memset(flag,0,sizeof(flag));
	int te=begin[1];
	while(to[te]!=-1){
		if(flag[to[te]]==0)
			q.push((p){te,weight[te]});
		te=next[te];
	}
	flag[1]=1;
	int ans=0;
	while(1){
		int flag2=1;
		for(int i=1;i<=n;i++){
			if(flag[i]==0){
				flag2=0;
				break;
			}
		}
		if(flag2==1)
			break;
		while(flag[to[q.top().edge]]==1)
			q.pop();
		p now=q.top();
		q.pop();
		ans=ans+now.weight;
		flag[to[now.edge]]=1;
		int te=begin[to[now.edge]];
		while(te!=-1){
			if(flag[to[te]]==0)
				q.push((p){te,weight[te]});
			te=next[te];
		}
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(false);
	#ifdef Kongxiangzhouye
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	while(cin>>n){
		memset(begin,-1,sizeof(begin));
		memset(next,-1,sizeof(next));
		memset(to,-1,sizeof(to));
		e=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int te;
				cin>>te;
				if(te!=-1)
					add(i,j,te);
			}
		}
		cout<<createtree()<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>
// #include <array>

using namespace std;

class MSTree
{
public:
  int solve();
};

int MSTree::solve()
{
  int n;
  cin >> n;
  
  vector<vector<int>> adj(n, vector<int>(n));
  list<int> t, vt;

  for ( auto i = 0; i < n; ++i )
    for ( auto j = 0; j < n; ++j )
      cin >> adj[i][j];

  t.push_back( 0 );

  for ( auto i = 1; i < n; ++i )
    vt.push_back( i );

  int w = 0;

  while ( vt.size() )
    {
      int mw = 4000;
      list<int>::iterator itm;
      
      for ( auto ita = t.begin(); ita != t.end(); ++ita )
        for ( auto itb = vt.begin(); itb != vt.end(); ++itb )
          {
            if ( adj[*ita][*itb] >= 0 && mw > adj[*ita][*itb] )
              {
                itm = itb;
                mw = adj[*ita][*itb];
              }
          }
      
      w += mw;
      t.push_back( *itm );
      vt.erase( itm );
    }

  cout << w << endl;
    
  return 0;
}

int main()
{
  MSTree m;

  m.solve();

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<utility>
#include<vector>

using namespace std;



int main() {
	int n,tmp;
	cin >> n;
	pair<int, pair<int, int>>* sorted_weight = new pair<int, pair<int, int>>[n * n];
	int* equiv = new int[n];


	int cnt_equiv = 0;
	int cnt_edge = 0;
	int cost = 0;
	
	for (int i = 0;i < n;i++) {
		for (int j = 0;j < n;j++) {
			cin >> tmp;
			sorted_weight[i * n + j] = make_pair(tmp, make_pair(i, j));
		}
		equiv[i] = 0;
	}
	sort(sorted_weight, sorted_weight + n * n);
	for (int i = upper_bound(sorted_weight, sorted_weight + n * n, make_pair(-1, make_pair(n, n))) - sorted_weight;cnt_edge<n-1;i++) {
		if (equiv[sorted_weight[i].second.second] == 0 || equiv[sorted_weight[i].second.first] == 0) {
			cnt_edge++;
			cost += sorted_weight[i].first;
			if (equiv[sorted_weight[i].second.second] == 0 && equiv[sorted_weight[i].second.first] == 0) {
				cnt_equiv++;
				equiv[sorted_weight[i].second.second] = equiv[sorted_weight[i].second.first] = cnt_equiv;
			}
			else if (equiv[sorted_weight[i].second.second] == 0) {
				equiv[sorted_weight[i].second.second] = equiv[sorted_weight[i].second.first];
			}
			else {
				equiv[sorted_weight[i].second.first] = equiv[sorted_weight[i].second.second];
			}
		}
		else if (equiv[sorted_weight[i].second.second] != equiv[sorted_weight[i].second.first]) {
			cnt_edge++;
			cost += sorted_weight[i].first;
			tmp = equiv[sorted_weight[i].second.second];
			for (int j = 0;j < n;j++) {
				if (equiv[j] == tmp)equiv[j] = equiv[sorted_weight[i].second.first];
			}
		}
	}
	
	cout << cost << endl;
}