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<cstdio>
static const int kMaxVertices = 100;
static const int kInfinity = 3000;
static int Adjacency_matrix[kMaxVertices][kMaxVertices];

void Prim(int n){
	bool has_not_been_discovered[kMaxVertices];
	int lowest_cost_edge[kMaxVertices];
	int parent[kMaxVertices];
	for (int i = 0; i < n; ++i)	{
		has_not_been_discovered[i] = true;
		lowest_cost_edge[i] = kInfinity;
	}
	lowest_cost_edge[0] = 0;
	parent[0] = -1;

	while(true) {
		int mincost = kInfinity;
		int vertex_to_add;
		for (int i = 0; i < n; ++i)	{
			if(has_not_been_discovered[i] && lowest_cost_edge[i] < mincost){
				mincost = lowest_cost_edge[i];
				vertex_to_add = i;
			}
		}

		if(mincost == kInfinity)
			break;

		has_not_been_discovered[vertex_to_add] = false;
		int vertex_added = vertex_to_add;

		for (int i = 0; i < n; ++i)	{
			if(has_not_been_discovered[i] && Adjacency_matrix[vertex_added][i] < lowest_cost_edge[i]) {
				lowest_cost_edge[i] = Adjacency_matrix[vertex_added][i];
				parent[i] = vertex_added;
			}
		}
	}
	int total_cost = 0;
	for (int i = 0; i < n; ++i)
		total_cost += lowest_cost_edge[i];
	printf("%d\n", total_cost);
}

int main() {
	int number_of_vertices;
	scanf("%d", &number_of_vertices);
	for (int vertex_from = 0; vertex_from < number_of_vertices; ++vertex_from) {
		for (int vertex_to = 0; vertex_to < number_of_vertices; ++vertex_to)	{
			int tmp;
			scanf("%d", &tmp);
			Adjacency_matrix[vertex_from][vertex_to] = (tmp < 0) ? kInfinity : tmp;
		}
	}
	Prim(number_of_vertices);
	return 0;
}

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 <utility>
#include <algorithm>
using namespace std;
const int MAX = 10005;
int P[MAX];
pair<int, pair<int,int> > C[MAX]; //<重み,<始点,終点>>

void init()
{
	for (int i = 0; i < MAX; ++i)
	{
		P[i] = i;
	}
}

int root(int a)
{
	if (a == P[a])
	{
		return a;
	}
	else
	{
		root(P[a]);
	}
}

bool same(int a, int b)
{
	if (root(a) == root(b))
	{
		return true;
	}
	else
	{
		return false;
	}
}

void unite(int a, int b)
{
	P[root(b)] = root(a); 
}

int main()
{
	int n;
	cin >> n;
	int cnt = 0;
	int temp;
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			cin >> temp;
			if (temp != -1)
			{
				C[cnt].first = temp;
				C[cnt].second.first = i;
				C[cnt].second.second = j;
				cnt++;
			}
		}
	}

	init();
	int ans = 0;
	sort(C,C+cnt);
	for (int i = 0; i < cnt; ++i)
	{
		if (same(C[i].second.first,C[i].second.second) == false)
		{
			ans += C[i].first;
			unite(C[i].second.first,C[i].second.second);
		}
	}

	cout << ans << endl;
	return 0;
}

C++: 
#include<stdio.h>

#define MAX 100
#define INFINITY 10000

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

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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define rep(i,n) for(int i=0;i<(int)n;i++)
const int INF = 1e9;

struct edge{ int u,v,cost;};
vector<edge> E;
int n;

struct UnionFind {
  vector< int > data;
 
  UnionFind(int sz) {
    data.assign(sz, -1);
  }
 
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }
 
  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }
 
  int size(int k) {
    return (-data[find(k)]);
  }
};


bool comp(const edge& e, edge& f){
    return e.cost < f.cost;
}


int main(){
    cin >> n;
    rep(i,n){
        rep(j,n){
            int c ; cin >> c;
            if(c!=-1){
                edge e = {i,j,c};
                E.push_back(e);
            }
        }
    }
    UnionFind T(n);
    sort(E.begin(),E.end(),comp);
    ll res = 0;
    rep(i,E.size()){
        edge e = E[i];
        if(T.find(e.u)!=T.find(e.v)) {
            T.unite(e.u,e.v);
            res+=e.cost;
        }
    }
    cout << res << endl;
}