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
	num := 0
	for len(edges) > 0 && num < n-1 {
		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
		num += 1
	}
	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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
static const int MAX = 100;
static const int INFTY = (1 << 30);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int n, M[MAX][MAX]; //nは要素数、Mは隣接行列
int prim()
{
    int u, minv;                    //uは最短距離の相手、minvは最短距離
    int d[MAX], p[MAX], color[MAX]; //dは始点からの距離、pは親,colorは訪問状態
    rep(i, 0, n)                    //初期設定
    {
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE; //未訪問
    }
    d[0] = 0; //始点のdは0
    while (1)
    {
        minv = INFTY;
        u = -1;
        rep(i, 0, n) //未完了の点のうち始点からの最短距離は？
        {
            if (minv > d[i] && color[i] != BLACK) //未完了の点がある
            {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1)
            break;
        color[u] = BLACK;
        rep(i, 0, n)
        {
            if (color[i] != BLACK && M[u][i] != INFTY)
            {
                if (d[i] > M[u][i])
                {
                    d[i] = M[u][i];
                    p[i] = u;
                    color[i] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    rep(i, 0, n)
    {
        if (p[i] != -1)
            sum += M[i][p[i]];
    }
    return sum;
}
int main()
{
    cin >> n;
    rep(i, 0, n)
    {
        rep(j, 0, n)
        {
            int e;
            cin >> e;
            M[i][j] = (e == -1) ? INFTY : e;
        }
    }
    cout << prim() << endl;
}

C++: 

#include <iostream>
using namespace std;
#define INFTY  (1 << 30)//极大值
#define NIL  -1
const int TOTAL = 101;
int totalnode,color[TOTAL], matrix[TOTAL][TOTAL], DistanceBetweenParent[TOTAL], parent[TOTAL];//颜色0白色，1为灰色，2是黑色
void includeMinimumSpanningTree()
{
	int u,min;
	DistanceBetweenParent[0] = 0;//这个点到其父节点的距离为0
	parent[0] = -1;//根的父节点为-1
	while (1)
	{
		min = INFTY;
		for (int i = 0; i < totalnode; i++)//找出非黑色同时到父节点加权值最少的节点
			if (color[i] != 2 && DistanceBetweenParent[i] < min)//第一次只有根
				min = DistanceBetweenParent[i], u = i;//以u寻找连接它的其他点
		color[u] = 2;//u的颜色为黑色
		if (min == INFTY)break;//寻找结束
		for (int i = 0; i < totalnode; i++)//寻找相连的点
		{
			if (color[i] != 2 && matrix[u][i]!= -1)//未发现且存在边
			{
				if (matrix[u][i] < DistanceBetweenParent[i])//当前点连接父节点的加权值大于新的加权值
				{
					color[i] = 1;//灰色
					DistanceBetweenParent[i] = matrix[u][i];
					parent[i] = u;
				}
			}
		}
	}
}
int main()
{	
	int inputdata,minmum=0;
	cin >> totalnode;
	for (int i = 0; i < totalnode; i++)color[i] = 0;//颜色初始化
	for(int i = 0; i < totalnode; i++)DistanceBetweenParent[i] = INFTY;//将距离初始化为最大值
	for (int i = 0; i < totalnode; i++)//顶点i 到顶点j的距离，不存在的为-1；
		for (int j = 0; j < totalnode; j++)
			cin >> inputdata, matrix[i][j] = inputdata;
	includeMinimumSpanningTree();
	for (int i = 0; i < totalnode; i++)
		minmum += DistanceBetweenParent[i];
	cout << minmum << endl;

}

C++: 
#include<stdio.h>

#define MAX 100
#define INFINITY 10000

int n;
int M[MAX][MAX], color[MAX], d[MAX], p[MAX];

enum {
    WHITE,
    GRAY,
    BLACK
};

int prim() {
    for (int i = 0; i < n; ++i) {
        color[i] = WHITE;
        d[i] = INFINITY;
        p[i] = -1;
    }
    d[0] = 0;
    p[0] = -1;

    while (true) {
        int min = INFINITY;
        int target = -1;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min && color[i] != BLACK) {
                min = d[i];
                target = i;
            }
        }
        if (target == -1) break;

        color[target] = BLACK;

        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && M[target][v] > 0) {
                if(M[target][v] < d[v]) {
                    d[v] = M[target][v];
                    p[v] = target;
                    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() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &M[i][j]);
        }
    }
    printf("%d\n", prim());
    return 0;
}

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

#define INF 1<<21
#define BLACK 0
#define GRAY 1
#define WHITE 2

int prim(int n, int **A, int *d, int *p) {
  int *color = new int[n];

  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    d[i] = INF;
  }

  d[0] = 0;
  p[0] = -1;

  while (true) {
    int mincost = INF;
    int u;
    for (int i = 0; i < n; i++) {
      if ((color[i] != BLACK) && (d[i] < mincost)) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INF) {
      break;
    }

    color[u] = BLACK;

    for (int v = 0; v < n; v++) {
      if ((color[v] != BLACK) && (A[u][v] != INF)) {
        if (A[u][v] < d[v]) {
          d[v] = A[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }

  }

  delete color;

  return 0;
}

int main() {
  int n;
  cin >> n;

  int **A = new int*[n];
  for (int i = 0; i < n; i++) {
    A[i] = new int[n];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int w;
      cin >> w;
      if (w == -1) {
        A[i][j] = INF;
      }
      else {
        A[i][j] = w;
      }
    }
  }

  int *d = new int[n];
  int *p = new int[n];
  prim(n, A, d, p);

  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += d[i];
  }

  cout << sum << endl;

  for (int i = 0; i < n; i++) {
    delete[] A[i];
  }
  delete[] A;

  delete d;
  delete p;

  return 0;
}

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

using namespace std;

struct Path{
    int w,s,t;
    
    bool operator < (const Path &a) const{
        return w < a.w;
    }
};

struct UnionFind{
    vector<int> par;
    
    UnionFind(int n)
        :par(n)
    {
        for(int i=0;i<n;i++){
            par[i]=i;
        }
    }
    
    void unite(int x,int y){
        int rx=root(x),ry=root(y);
        if(rx==ry)return;
        else{
            par[rx]=ry;
            return;
        }
    }
    
    int root(int x){
        if(x==par[x])return x;
        else return par[x]=root(par[x]);
    }
};

int main(void){
    int i,j,n,w;
    vector<Path> path;
    Path tmp;
    
    cin>>n;
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            cin>>w;
            if(i>j)continue;
            else if(w!=-1){
                tmp.w=w,tmp.s=i,tmp.t=j;
                path.push_back(tmp);
            }
        }
    }
    
    sort(path.begin(),path.end());
    
    UnionFind tree(n);
    int sum=0;

    for(i=0;i<path.size();i++){
        tmp=path[i];
        if(tree.root(tmp.s)!=tree.root(tmp.t)){
            tree.unite(tmp.s,tmp.t);
            sum+=tmp.w;
        }
    }

    cout<<sum<<endl;
    return 0;
}