Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] > 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A > edges[j].A })

	sum := 0
	for len(edges) > 0 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

C++: #include <iostream>
#include <vector>
#include <numeric>

using namespace std;

const int N = 100;

/*
// ????????????
Vnew = {x}???????????§ x ??? V ????????§??????????????????????????????????????????
Enew = {}
while (Vnew ??? V)
    Vnew ????????????????????? u ??¨ ????????????????????? v ????????¶?????????????°??????? (u, v) ??? E ????????????
    v ??? Vnew ????????????
    (u, v) ??? Enew ????????????
*/

enum Color : int {
    WHITE,
    GRAY,
    BLACK,
};

int main() {
    int A[N][N];

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int a;
            cin >> a;
            A[i][j] = a == -1 ? a : numeric_limits<int>::max();
        }
    }

    vector<int> color(n, WHITE);
    vector<int> d(n, numeric_limits<int>::max());
    vector<int> p(n, -1);

    d[0] = 0;
    while (1) {
        int minv = numeric_limits<int>::min();
        int 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 && A[u][v] != numeric_limits<int>::max()) {
                if (d[v] > A[u][v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum;
    for (int i =0; i < n; ++i) {
        if (p[i] != -1)
            sum += A[i][p[i]];
    }
    cout << sum << endl;

    return 0;
}

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

static const int MAX_NUM  = 101;
static const int NO_VISIT  = 0;
static const int NOW_VISIT = 1;
static const int END_VISIT = 2;

void MPT(int *pElem, int num)
{
    int i;
    int mincost;
    int target;
    int parent[MAX_NUM];
    int cost[MAX_NUM];
    int state[MAX_NUM] = {0};
    int *tmp;
    int total = 0;

    for (i = 0; i < num; i++) {
        parent[i] = -1;
        cost[i]   = 0x7FFFFFFF;
    }

    cost[0] = 0;

    for (;;) {
        mincost = 0x7FFFFFFF;
        for (i = 0; i < num; i++) {
            if ((state[i] != END_VISIT) && (mincost > cost[i])) {
                target  = i;
                mincost = cost[i];
            }
        }
        if (mincost == 0x7FFFFFFF) {
            break;
        }

        state[target] = END_VISIT;
        total += mincost;

        tmp = pElem + target * MAX_NUM;
        for (i = 0; i < num; i++) {
            if ((state[i] != END_VISIT) && (*(tmp + i) < cost[i])) {
                cost[i]   = *(tmp + i);
                parent[i] = target;
                state[i]  = NOW_VISIT;
            }
        }
    }

    printf("%d\n", total);

    return;
}

int main(void)
{
    int i, j;
    int num;
    int tmp;
    int elem[MAX_NUM][MAX_NUM];

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        for (j = 0; j < num; j++) {
            scanf("%d", &tmp);
            elem[i][j] = (tmp != -1) ? tmp : 0x7FFFFFFF;
        }
    }

    MPT(&elem[0][0], num);

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

static const int N=100;
static const int INF=100000;

int n;
int M[N][N];

//初期化
int prim(){
  int u,minv;
  int d[N],p[N],color[N];
  for(int i=0;i<n;i++){
    d[i]=INF;
    p[i]=-1;
    color[i]=0;
  }
  
  d[0]=0;
  
  while(1){
    minv=INF;
    u=-1;
    for(int i=0;i<n;i++){
      if(minv>d[i]&&color[i] != 2){
        u = i;
        minv = d[i];
      }
    }
    if(u == -1) break;
    color[u] = 2;
    
    for(int v=0;v<n;v++){
      if(color[v] !=2 && M[u][v] != INF){
        if(d[v]>M[u][v]){
          d[v]=M[u][v];
          p[v]=u;
          color[v]=1;
        }
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    if(p[i] !=-1 ) sum += M[i][p[i]];
  }
  return  sum;
}

int main(){
  int e; 
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>e;
      M[i][j]=(e== -1)? INF : e;
    }
  }
  
  cout<<prim()<<endl;
  return 0;
}

C++: #include <iostream>
using namespace std;
const int maxx=110;
const int infinity=(1<<30);
 
int A[maxx][maxx];
bool flag[maxx];	//用flag来标记这个节点是不是在MST中，true是在T里，false是在V-T里；
 
int main (){
		int n;
		cin>>n;
		for(int i =1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int e;cin>>e;
				A[i][j]=(e==-1)?infinity:e;
			}
		}
 
		for(int i=0;i<maxx;i++) flag[i]=false;
		int sum=0,minv,minj;
		flag[1]=true;
		while(1){
			minv=infinity;
			minj=-1;
			for(int i=1;i<=n;i++){
				if(flag[i]==true){//找到一个在T中的顶点
					for(int j=1;j<=n;j++){
						if(A[i][j]!=-1 && A[i][j]<minv && flag[j]==false) {//找到一个在V-T中的顶点
							minv=A[i][j];//找到权值的最小值minv
							minj=j;//找到权值的最小值对应的顶点，就是下一步要加入到MST中的顶点minj
						} 
					}
				}
			}
//			cout<<minj<<endl;
			if(minv==infinity || minj==-1) break;
			flag[minj]=true;
			sum+=minv;
		}
		
		cout<<sum<<endl;
		return 0;
		
}

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

const int MAX = 100;


int main() {
	int n;
	int graph[MAX][MAX];
	bool table[MAX] = { false };
	int sum = 0;

	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int temp;
			cin >> temp;
			if (temp == -1)temp = INT_MAX;
			graph[i][j] = temp;
		}
	}

	table[0] = true;

	for (int i = 0; i < n - 1; i++) {
		int cost = INT_MAX;
		int m;

		for (int j = 0; j < n; j++) {
			if (table[j] == false)continue;

			for (int k = 0; k < n; k++) {
				if (graph[j][k] < cost && table[k] == false) {
					cost = graph[j][k];
					m = k;
				}
			}
		}

		sum += cost;
		table[m] = true;
	}

	cout << sum << endl;

	return 0;
}