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;
const int MAX = 100;
const int INF = 20000;
int n, G[MAX][MAX], dis[MAX];
bool book[MAX] = { false };
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> G[i][j];
	for (int i = 0; i < n; i++)
		dis[i] = G[0][i];
	book[0] = true;
	int sum = 0;
	for (int k = 1; k <= n - 1; k++)
	{
		int min = INF;
		int u;
		for (int j = 0; j < n; j++)
			if (!book[j] && dis[j] != -1 && dis[j] < min)
			{
				min = dis[j];
				u = j;
			}
		book[u] = true;
		sum += dis[u];
		for (int v = 0; v < n; v++)
			if (!book[v] && G[u][v] != -1)
				if (dis[v] == -1)
					dis[v] = G[u][v];
				else
					if (dis[v] > G[u][v])
						dis[v] = G[u][v];
	}
	cout << sum << endl;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define to first
#define cost second
const int inf=1e9+7;
using namespace std;
int main(){

  int n,tmp,r=0,flag[100]={},v=0;
  cin>>n;
  vector <pair<int,int> > gl[n];
  priority_queue <pair<int,int> ,vector<pair<int,int> >, greater<pair<int,int> > >q;
  //for(int i=1;i<n;i++) mincost[i]=inf;                                                                                                                                                       

  for(int i=0;i<n;i++) for(int j=0;j<n&&cin>>tmp;j++) if(tmp!=-1) gl[i].push_back(make_pair(j,tmp));
  int now=0;
  for(int j=0;j<n-1;j++){
    for(int i=0;i<gl[now].size();i++) if(!flag[gl[now][i].to]) q.push(make_pair(gl[now][i].cost,gl[now][i].to));
    flag[now]=1;
    // cout<<now<<" "<<q.top().first<<endl;                                                                                                                                                    
    for(;flag[q.top().second];) q.pop();
    if(q.empty()) break;
    r+=q.top().first;
    now=q.top().second;
    q.pop();
  }
  cout<<r<<endl;
  return 0;

}

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

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 2000000000

int x[10000][10000];
int color[10000];
int MIN[10000];
int sum;
int sum_min = INF;

int main() {
	int n, a, b, c;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> x[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		memset(MIN, 127, sizeof(MIN));
		b = INF;
		a = 1;
		color[i] = GRAY;
		MIN[i] = 0;
		while (a >= 1) {
			for (int j = 0; j < n; j++) {
				if (color[j] = GRAY) {
					if (b > MIN[j]) {
						b = MIN[j];
						c = j;
					}
				}
			}
			color[c] = BLACK;
			a -= 1;
			for (int j = 0; j < n; j++) {
				if (color[j] != BLACK && x[c][j] != -1) {
					if (color[i] == WHITE) {
						a += 1;
					}
					color[j] = GRAY;
					MIN[j] = min(x[c][j], MIN[j]);
				}
			}
		}
		sum = 0;
		for (int j = 0; j < n; j++) {
			sum += MIN[j];
		}
		sum_min = min(sum_min, sum);
	}
	cout << sum_min << endl;
	return 0;
}