Go: package main

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

const (
	MAX_N      = 200
	MAX_WEIGHT = 2000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		adjacentMatrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			v, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjacentMatrix[i][j] = v
		}
	}

	mst([]int{1}, shortestList, adjacentMatrix)
	fmt.Println(shortestList)
	fmt.Println(calculateMinWeight(shortestList))
}

func calculateMinWeight(shortestList []ShortestInfo) int {
	visitedList := make([]bool, len(shortestList))
	visitedList[0] = true
	sumWeight := 0
	for !visitedAll(visitedList) {
		shortestInfo := shortestList[0]
		shortestList = shortestList[1:]
		edges := shortestInfo.edges
		for _, edge := range edges {
			if !visitedList[edge.end-1] {
				visitedList[edge.end-1] = true
				sumWeight += edge.weight
			}
		}
	}
	return sumWeight
}

func visitedAll(visitedList []bool) bool {
	for _, v := range visitedList {
		if !v {
			return false
		}
	}
	return true
}

func mst(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			s := getShortest(adj.end, shortestList)
			if currentShortest.shortest+adj.weight < s.shortest || (currentShortest.shortest+adj.weight == s.shortest && len(currentShortest.edges)+1 > len(s.edges)) {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node-1]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node-1] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode-1]
	edges := []Edge{}
	for i, v := range adjacents {
		if v > 0 {
			edges = append(edges, Edge{start: currentNode, end: i + 1, weight: v})
		}
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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;

}