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<algorithm>
#define MAX 101
#define INF 1<<21
#define NIL -1
using namespace std;
int n,A[MAX][MAX];
int Minimum_Spanning_Tree(void){
    int color[MAX],d[MAX],vertices,mincost;
    for(int i=0;i<n;i++){
        color[i] = 0;
        d[i] = INF;
    }
    d[0] = 0;
    while(true){
        mincost = INF;
        vertices = NIL;
        for(int i=0;i<n;i++){
            if(color[i]!=1 & d[i]<mincost){
                mincost = d[i];
                vertices = i; 
            }
        }
        if(vertices == NIL){
            break;
        }
        color[vertices] = 1; 
        for(int i=0;i<n;i++){
            if(color[i]!=1 && A[vertices][i] != INF){
                if(A[vertices][i] < d[i]){
                    d[i] = A[vertices][i];
                }
            }
        }
    }
    int sum = 0;
    for(int i=0;i<n;i++){
        sum += d[i];
    }
    return sum;
}
int main(void){
    cin >> n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> A[i][j];
            if(A[i][j] == NIL){
                A[i][j] = INF;
            }
        }
    }
    cout << Minimum_Spanning_Tree() << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct UnionFind{
    vector<int> parent;

    UnionFind(int n) : parent(n){
        for (int i = 0; i < n; i++)parent[i] = i;
    }

    int find(int e){
        if (parent[e] == e) return e;
        else return parent[e] = find(parent[e]);
    }

    void unite (int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b) return;
        parent[a] = b;
    }

    bool same(int a, int b){
        return find(a) == find(b);
    }
};

int main() {
    vector<tuple<int, int, int>> a;
    int n, t, ans = 0;
    cin >> n;
    UnionFind uf(n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> t;
            if (i <= j && t != -1) a.push_back(make_tuple(t,i,j));
        }
    }
    sort(a.begin(), a.end(), [](const tuple<int, int, int>& x, const tuple<int, int, int>& y) { return get<0>(x) < get<0>(y);} );
    for(auto e : a){
        if( !uf.same(get<1>(e), get<2>(e)) ){
            ans += get<0>(e);
            uf.unite(get<1>(e), get<2>(e));
        }
    }
    cout << ans << endl;
}

C++: #include <iostream>

const int MAX = 101;
const int NIL = -1; 
const int UPBOUND = (1 << 20);

using namespace std;

int prim(int adj_matrix[][MAX], int n, int u);


int main()
{
	int adj_matrix[MAX][MAX] = {0};		
	
	int n;
	cin >> n;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			int e; cin >> e;
			adj_matrix[i][j] = e == -1? UPBOUND: e;
		}
	}
	
//	for (int i = 0; i < n; i++)
//	{
//		for (int j = 0; j < n; j++)
//		{
//			cout << adj_matrix[i][j] << " ";
//		}
//		cout << endl;
//	}

	
	cout << prim(adj_matrix, n, 1) << endl;
} 


int prim(int adj_matrix[][MAX], int n, int u)
{
	int cost[MAX]; //记录最小权值
	int edge[MAX]; //记录选中的边
	
	
	for (int v = 1; v <= n; v++)
	{
		cost[v] = adj_matrix[u][v];
		edge[v] = u;
	}	 
	
	cost[u] = NIL; //排除u 
	
	while (true)
	{
		int w = NIL;
		int min = 3000; //权值上限 
		for (int v = 1; v <= n; v++)
		{
			if (cost[v] != NIL && cost[v] < min)
			{
				min = cost[v];
				w = v;
			} 
		}
		
		if (w == NIL) break;
		
		cost[w] = NIL;
		
		//更新最小权值数组 
		for (int v = 1; v <= n; v++)
		{
			if (cost[v] > adj_matrix[w][v] && adj_matrix[w][v] != NIL)
			{
				cost[v] = adj_matrix[w][v];
				edge[v] = w;			
			}
		}
				
	}
	
	
	//求生成树的权值
	int sum = 0;
	for (int v = 1; v <= n; v++)
	{
		if (v != u)
		sum += adj_matrix[v][edge[v]];
	} 
	
	return sum;	
	
}

C++: #include <iostream>
#include <limits>

constexpr int MAXN = 100;
int G[MAXN][MAXN];
int P[MAXN];
int D[MAXN];
int C[MAXN];

int main(){
    int n;
    std::cin >> n;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            std::cin >> G[i][j];
            if (G[i][j] == -1) {
                G[i][j] = std::numeric_limits<int>::max();
            }
        }
    }

    for (int i = 0; i < n; i++) {
        D[i] = std::numeric_limits<int>::max();
    }

    D[0] = 0;
    P[0] = -1;
    while (true) {
        int m;
        int mincost = std::numeric_limits<int>::max();
        for (int i = 0; i < n; i++) {
            if ((C[i] == 0) && (D[i] < mincost)) {
                mincost = D[i];
                m = i;
            }
        }

        if (mincost == std::numeric_limits<int>::max()) {
            break;
        }

        C[m] = 1;
        D[m] = mincost;

        for (int i = 0; i < n; i++) {
            if ((G[m][i] < std::numeric_limits<int>::max()) && (C[i] == 0) && (G[m][i] < D[i])) {
                D[i] = G[m][i];
            }
        }
    }


    int sum = 0;
    for (int i = 0; i < n; i++) {
        if (D[i] != std::numeric_limits<int>::max()) {
            sum += D[i];
        }
    }
//    for (int i = 0; i < n; i++) {
//        std::cout << i << ": " << D[i] << std::endl;
//    }
    std::cout << sum << std::endl;

}

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 (true) {
		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 (color[v] != BLACK && M[u][v] != INFTY) {
				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++) {
			cin >> M[i][j];
			if (M[i][j] == -1)M[i][j] = INFTY;
		}
	}
	cout << prim() << '\n';
	return 0;
}