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>
#include <vector>
#include <algorithm>

static int n;
static std::vector<std::vector<int>> link_mat;

enum STATUS {
    IN,
    OUT,
    EDGE,
};


void updateEdge(int node, std::vector<STATUS> &statuses) {
    statuses[node] = STATUS::IN;
    for (int i = 0; i < link_mat[node].size(); i++) {
        if (link_mat[node][i] == -1) {
            continue;
        }
        if (statuses[i] == STATUS::OUT) {
            statuses[i] = STATUS::EDGE;
        }
    }
}


int search(int node, std::vector<STATUS> &statuses) {
    updateEdge(node, statuses);
    int min = 2001;
    int min_node = -1;
    for (int i = 0; i < n; i++) {
        if (statuses[i] != STATUS::EDGE) {
            continue;
        }
        for (int j = 0; j < n; j++) {
            int w = link_mat[i][j];
            if (w == -1 || w > min) {
                continue;
            }
            if (statuses[j] != STATUS::IN) {
                continue;
            }
            min = w;
            min_node = i;
        }
    }
    if (min_node == -1) {
        return 0;
    }
    return min + search(min_node, statuses);
}


int main() {
    std::cin >> n;
    link_mat = std::vector<std::vector<int>>(n, std::vector<int>(n));
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cin >> link_mat[i][j];
        }
    }
    std::vector<STATUS> statuses(n, STATUS::OUT);
    int cost = search(0, statuses);
    
    std::cout << cost << std::endl;
}

C++: #include <iostream>
#include <utility>
#include <string>
#include <algorithm>

using namespace std;


int P[10010]; // 0 ?????? 10000 ?????§????????????????????±?????????
void init(int N) { // ????????? ??????????????¨???????????????????????????
  for (int i=0; i<=N; ++i) P[i] = i;
  // ??¨: ????????? [0,N-1] ????????? [1,N] ?????????????????????
  // ???????????§???????????§???????????????????????§??? [0,N] ???????????¨??????
}
int root(int a) { // a ??? root(??£??¨???) ????±???????
  if (P[a] == a) return a; // a ??? root
  return (P[a] = root(P[a])); // a ????????? root ????±???????a ????????¨??????
}
bool is_same_set(int a, int b) { // a ??¨ b ???????????°?????????????±???????????
  return root(a) == root(b);
}
void unite(int a, int b) { // a ??¨ b ???????????°?????????????????¨??????
  P[root(a)] = root(b);
}


int N=0;
int A[100+10][100+10] = {0};

int cost[10100] = {0};
int src[10100] = {0};
int dst[10100] = {0};

pair<int,int> c_n[10100];



int main() {
  cin >> N;
  init(N+10);
  
  int k=0;
  for(int j=0;j<N;++j){
    for(int i=0;i<N;++i){
      cin >> A[i][j];
      //cout << A[i][j] << "|";
      if (A[i][j] != -1){
	cost[k] = A[i][j];
	src[k] = i;
	dst[k] = j;
	c_n[k] = make_pair(cost[k],k);
	++k;
      }
    }
    //cout << endl;
  }


  sort(c_n, c_n+k);

  

  int num = 0;
  int sum = 0;
  for(int i=0;i<k;++i){
    num = c_n[i].second;
    if(is_same_set(src[num], dst[num]) == 0){
      unite(src[num], dst[num]);
      sum += cost[num];
      //cout << num << " " << sum << endl;
    }
  }
  cout << sum << endl;
}

C++: #include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ROOP1(i,n) for(int i=0;i<n;i++)
#define ROOP2(j,n) for(int j=0;j<n;j++)
#define Psi pair<string,int>
#define Pis pair<int,string>
#define Pii pair<int,int>
static const int INFTY = (1 << 21);
static const int MAX = 100;
static const int WHITE = 1;
static const int GRAY = 2;
static const int BLACK = 3;
// ??????????????????memset(a,0,sizeof(a));
// ?´???°?????? if(x<2)return0;else if(x==2)return1;if(x%2==0)return0;for(i=3;i*i<=x;i+=2){if(x%i==0)return0;}return1;}p440
//vector size();push_back(x);pop_back();begin();end();insert(p,x);erase(p);clear();
//stack  size();top();pop();push(x);emtpy();
//queue  size();front();pop();push(x);empty();
int n;
int M[MAX][MAX];
int color[MAX], d[MAX], p[MAX];

int main(){
	cin >> n;
	ROOP1(i, n){
		ROOP2(j, n){
			cin >> M[i][j];
		}
	}
	for (int i = 0; i < n; i++){
		d[i] = INFTY;
		color[i] = WHITE;
		p[i] = -1;
  	}
	bool flag =  true;
	d[0] = 0;
	while (flag){
		int mincost = INFTY; int u = 0;
		for (int i = 0; i < n; i++){
			if (color[i] != BLACK && d[i] < mincost){
				mincost = d[i];
				u = i;
			}
		}
		if (mincost == INFTY) break;
		color[u] = BLACK;
		for (int v = 0; v < n; v++){
			if (M[u][v] != -1 && color[v] != BLACK){
				if (d[v] > M[u][v]){
					d[v] = M[u][v];
					p[v] = u;
					color[v] = GRAY;
				}
			}
		}
 	}
	int sum = 0;
	for (int i = 0; i < n; i++){
		sum += M[i][p[i]];
	}
	cout << sum << endl;
	return 0;
}

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

int prim() {
	int u, minv;
	int d[MAX], p[MAX], color[MAX];
	for (int i = 0; i < n; i++)
	{
		d[i] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
	}
	d[0] = 0;
	while (1)
	{
		minv = INFTY;
		u = -1;
		for (int i = 0; i < n; i++)
		{
			if (minv > d[i] && color[i] != BLACK) {
				u = i;
				minv = d[i];
			}
		}
		if (u == -1) break;
		color[u] = BLACK;
		for (int v = 0; v < n; v++)
		{
			if (d[v] > M[u][v]) {
				d[v] = M[u][v];
				p[v] = u;
				color[v] = GRAY;
			}
		}
		int sum = 0;
		for (int i = 0; i < n; i++)
		{
			if (p[i] != -1) sum += M[i][p[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;
				M[i][j] = (e == -1) ? INFTY : e;
			}
		}
		cout << prim() << endl;
		return 0;
	}