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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define N 110
#define INF 0x3f3f3f3f
using namespace std;
typedef struct {
    int w;
    int s;
    int e;
}Edge;
Edge edge[N*N];
int m[N][N];
int father[N];
int n;
int k = 0;  //edge的下标
int result = 0;
int findfather(int x){
    return x == father[x] ? x : findfather(father[x]);
}
int cmp(Edge a, Edge b){
    return a.w < b.w;
}
void Kruscal(){
    for(int u = 0; u < k; u++){
        int s = edge[u].s;
        int e = edge[u].e;
        if(findfather(s) != findfather(e)){
            father[findfather(e)] = s;
            result += edge[u].w;
        }
    }
    printf("%d\n", result);
}
int main(){

    scanf("%d", &n);
    for(int i = 1; i <= n; i++) father[i] = i;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            int b;
            scanf("%d", &b);
            m[i][j] = b;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n ;j++){
            if(m[i][j] != -1) {
                edge[k].w = m[i][j];
                edge[k].s = i;
                edge[k].e = j;
                k++;
            }
        }
    }
    sort(edge, edge+k, cmp);
    Kruscal();
    return 0;
}

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

using namespace std;

using pii = pair<int, int>;

struct Graph{
  int v_size;
  vector<vector<pii>> adj;
  
  Graph(const int v_size):v_size(v_size){
    this->adj.resize(v_size);
  }
  
  void add_edge(const int s, const int t, const int w){
    this->adj[s].emplace_back(pii(t, w));
  }
};

int mst(const Graph& g){
  constexpr int INF{INT_MAX};
  
  vector<int> d(g.v_size, INF);
  priority_queue<pii, vector<pii>, greater<pii>> pq;

  d[0] = 0;
  pq.push(pii(0, 0));

  int ans {0};
  
  while(!pq.empty()){
    auto cost {pq.top().first};
    auto v {pq.top().second};
    pq.pop();

    if(d[v] < cost) continue;

    ans += cost;
    d[v] = -INF;

    for(int i = 0; i < g.adj[v].size(); ++i){
      auto to {g.adj[v][i].first};
      auto next_cost {g.adj[v][i].second};

      if(d[to] > next_cost){
        d[to] = next_cost;
        pq.push(pii(next_cost, to));
      }
    }
  }

  return ans;
}

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

  int x;
  for(int i = 0; i < n; ++i){
    for(int j = 0; j < n; ++j){
      cin >> x;
      if(x != -1){
        g.add_edge(i, j, x);
      }
    }
  }

  cout << mst(g) << endl;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<20);
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++) {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    d[0] = 0;

    while (1) {
        minv = INFTY;
        for (int i=0; i<n; i++) {
            if (color[i]!=BLACK && d[i]<minv) {
                minv = d[i];
                u = i;
            }
        }

        if (minv == INFTY) {
            break; // All nodes visited
        }

        color[u] = BLACK;
        for (int v=0; v<n; v++) {
            if (color[v]!=BLACK && M[u][v]<INFTY) {
                if (M[u][v]<d[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]];
        // }
        sum += d[i];
    }

    return sum;
}

int main() {
    cin >> n;
    int e;
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cin >> e;
            if (e!=-1) {
                M[i][j] = e;
            } else {
                M[i][j] = INFTY;
            }
        }
    }
 
    cout << prim() << endl;
    return 0;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
int matrix[100][100];
pair<int,int> edges[100];
int cost[100];
int src[100];
int dst[100];
int p[10010];
void init(int n)
{
    for(int i=0;i<=n;i++)
        p[i]=i;
};
int root(int a)
{
    if(p[a]==a)
        return a;
    return (p[a]=root(p[a]));
};
bool is_same_set(int a,int b)
{
    return root(a)==root(b);
};
void unite(int a,int b)
{
    p[root(a)]=root(b);
};

int main()
{
    int n,amount=0,sum=0;
    
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>matrix[i][j];
        }
    }
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=i;j++)
        {
            if(matrix[i][j]!=-1)
            {
                cost[amount]=matrix[i][j];
                src[amount]=i;
                dst[amount]=j;
                amount++;
            }
        }
    }
    for(int i=0;i<amount;i++)
    {
        edges[i]=make_pair(cost[i],i);
    }

	init(n);
    
    sort(edges,edges+amount);
    
    for(int i=0;i<amount;i++)
    {
        if(is_same_set(src[edges[i].second()],dst[edges[i].second()]))
			continue;
		unite(src[edges[i].second()],dst[edges[i].second()]));
		sum+=edges[i].first();
    }
	cout<<sum<<endl;
}

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

int main(){
  int n;
  int G[100][100],d[100];
  int minc,mi,i;
  bool flg[i];
  int sum=0;
  
  cin >> n;
  for (i=0;i<n;i++){
    for (int j=0;j<n;j++){
      cin >> G[i][j];
      if (G[i][j]==-1) G[i][j]=INF;
    }
    d[i]=INF;
    flg[i]=false;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (!flg[i]&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flg[mi]=true;
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (!flg[i]&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    sum+=d[i];
  }
  cout << sum<<endl;
  
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<stack>
#include<vector>
#define INFTY 10000
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

int color[120],d[120];
int M[120][120];
int n;

int start(int x){
  d[x]=0;
  while(true){
    int minimumd=INFTY,u;
    for(int i=0;i<n;i++){
      if(color[i]!=BLACK && minimumd>d[i]){
        minimumd=d[i];
        u=i;
      }
    }
    if(minimumd==INFTY) break;
    color[u]=BLACK;
    for(int i=0;i<n;i++){
      if(d[i]>M[u][i] && color[i]!=BLACK){
        d[i]=M[u][i];
        color[i]=GRAY;
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    sum+=d[i];
  }
  return sum;
}

int main(){
  int temp;
  cin>>n;
  for(int i=0;i<n;i++){
    d[i]=INFTY;
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>temp;
      M[i][j]=(temp==-1)? INFTY : temp; 
    }
  }
  d[0]=0;
  int ans=start(0);
  cout<<ans<<endl;
}