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++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <climits>
#include <list>
#include <queue>

using namespace std;

static const int INFTY = 2001;

int H;
int A[101];
int NodeNum[101];
int IndexInA[100];


int parent(int i) {
	return i / 2;
}

int left(int i) {
	return 2 * i;
}

int right(int i) {
	return 2 * i + 1;
}

void minHeapify(int i) {
	int l = left(i);
	int r = right(i);
	int smallest = i;
	if (l <= H && A[l] < A[i]) {
		smallest = l;
	}
	if (r <= H && A[r] < A[smallest]) {
		smallest = r;
	}
	if (smallest != i) {
		int temp = A[i];
		A[i] = A[smallest];
		A[smallest] = temp;
		temp = NodeNum[i];
		NodeNum[i] = NodeNum[smallest];
		NodeNum[smallest] = temp;
		IndexInA[NodeNum[i]] = i;
		IndexInA[NodeNum[smallest]] = smallest;

		minHeapify(smallest);
	}
}

void insert(int d, int nodeNum) {
	int i = ++H;
	A[i] = d;
	NodeNum[i] = nodeNum;
	IndexInA[nodeNum] = i;
	int p = parent(i);
	while (i > 1 && A[p] > A[i]) {
		int temp = A[p];
		A[p] = A[i];
		A[i] = temp;
		temp = NodeNum[p];
		NodeNum[p] = NodeNum[i];
		NodeNum[i] = temp;
		IndexInA[NodeNum[p]] = p;
		IndexInA[NodeNum[i]] = i;
		i = p;
		p = parent(i);
	}
}

int heapExtractMinNodeNum() {
	int minNodeNum = NodeNum[1];
	A[1] = A[H];
	NodeNum[1] = NodeNum[H];
	IndexInA[NodeNum[1]] = 1;
	H--;
	minHeapify(1);

	return minNodeNum;
}

void decrese(int i, int value) {
	A[i] = value;
	int p = parent(i);
	while (i > 1 && A[p] > A[i]) {
		int temp = A[p];
		A[p] = A[i];
		A[i] = temp;
		temp = NodeNum[p];
		NodeNum[p] = NodeNum[i];
		NodeNum[i] = temp;
		IndexInA[NodeNum[p]] = p;
		IndexInA[NodeNum[i]] = i;
		i = p;
		p = parent(i);
	}
}

int main() {
	//int Dist[101];
	int Dist[100];
	int AdjacenceyList[100][100];
	int AdjacenceyWeightList[100][100];
	int Degree[100];
	int i, j;
	int N;
	int W;
	int Deg;
	int MinTotalWeight = 0;
	int u, v;
	bool inT[100];

	cin >> N;

	for (i = 0; i < N; i++) {
		inT[i] = false;
		Dist[i] = INFTY;
		Deg = -1;
		for (j = 0; j < N; j++) {
			cin >> W;
			if(W != -1){
				AdjacenceyList[i][++Deg] = j;
				AdjacenceyWeightList[i][Deg] = W;
			}
		}
		Degree[i] = Deg + 1;
	}
	//Dist[100] = INFTY;



	H = 0;
	u = 0;
	inT[u] = true;
	Dist[u] = 0;
	for (i = 1; i <= N - 1; i++) {
		for (j = 0; j < Degree[u]; j++) {
			v = AdjacenceyList[u][j];
			if (inT[v] == false) {
				W = AdjacenceyWeightList[u][j];
				if (Dist[v] == INFTY) {
					Dist[v] = W;
					insert(W, v);
				}
				else if (W < Dist[v]) {
					Dist[v] = W;
					decrese(IndexInA[v], W);
				}
			}
		}

		int minNodeNum = heapExtractMinNodeNum();
		inT[minNodeNum] = true;
		MinTotalWeight += Dist[minNodeNum];
		u = minNodeNum;

	}

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

	return 0;
}

C++: #include <iostream>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1<<21
#define N 100

int n, A[N][N];

int ans() {
	int color[N], p[N], d[N];
	int u, minv;

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

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

		if (u == -1) break;
		color[u] = BLACK;

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

	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) sum += A[i][p[i]];
	}
	return sum;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int e;
			cin >> e;
			A[i][j] = (e != -1) ? e : INFTY;
		}
	}
	cout << ans() << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INFTY (1<<30)

int n;
vector<int> color(101,0),d(101,INFTY),p(101);
vector<vector<int>> M(101,vector<int>(101,INFTY));

void prim(){
    d[0]=0;
    p[0]=-1;

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

        for(int v=0;v<n;v++){
            if(color[v]!=2&&M[u][v]<INFTY){
                if(M[u][v]<d[v]){
                    d[v]=M[u][v];
                    p[v]=u;
                    color[v]=1;
                }
            }
        }

    }

}

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>M[i][j];
            if(M[i][j]==-1) M[i][j]=INFTY;
        }
    }

    prim();

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

C++: #include <iostream>
#include <queue>
using namespace std;
int main() {
    int n;
    cin >> n;
    int a[100][100];
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j];
    struct Edge {
        int from, to, weight;
        Edge(int from, int to, int weight) : from(from), to(to), weight(weight) {}
        bool operator < (const Edge& e) const {
            return weight > e.weight;
        }
    };
    int sm = 0;
    priority_queue<Edge> q;
    q.push(Edge(-1, 0, 0));
    bool visited[100]{};
    while (q.size()) {
        auto e = q.top();
        q.pop();
        if (visited[e.to]) continue;
        visited[e.to] = true;
        sm += e.weight;
        for (int i = 0; i < n; ++i) {
            if (a[e.to][i] >= 0 && !visited[i])
                q.push(Edge(e.to, i, a[e.to][i]));
        }
    }
    cout << sm << endl;
    return 0;
}

C++: #include<stdio.h>
#include<limits.h>
#define NIL -1
static const int MAX = 500;
static const int INFTY = (1<<21);
int n, i, j, e, v, sum, u , minV;
main(){
 
  int M[MAX][MAX], key[MAX], L[MAX],A[MAX];
  
  scanf("%d", &n);
  
  for ( i = 0; i< n; i++ ){
    for ( j = 0; j < n; j++ ){
      scanf("%d", &e);
      M[i][j] = (e==-1)?INFTY:e;
    }
  }

  for(i = 0; i < n; i++){
    key[i] = INT_MAX;
    L[i] = NIL;
    A[i] = NIL;
  }
  
  key[0]=0;
  
  for(;;){
    
    u = NIL;
    
    minV = INT_MAX;
    
    for(i = 0;  i < n; i++){
      
      if(key[i] < minV && A[i] == NIL){
        
        u = i;
        
        minV = key[i];
        
      }
      
    }  
    if(u == NIL)break;
    
    A[u] = u; 
    
    for(v = 0; v < n; v++){
      if(M[u][v] < key[v] && A[v] == NIL){
        key[v] = M[u][v];
        L[v] = u;
      }
    }
  }
  
  sum = 0;
  
  for ( i = 0; i < n; i++ ){
    if ( L[i] != -1 ) sum +=M[L[i]][i];
  }
  
  printf("%d\n", sum);
  
  
  return 0;
}