Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

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

  d[0] = 0;

  while(1){
    minv = INFTY;
    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 && M[u][v] != INFTY){
        if(d[v] > M[u][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]];
  }

  return sum;
}

int main(){
  cin >> n;

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

  cout << prim() << endl;

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;

int par[101], d[101];
priority_queue<PP, vector<PP>, greater<PP> > G;

void init(int n){
	for(int i = 1;i <= n;i++){
		par[i] = i;
		d[i] = 0;
	}
}

int find(int x){
	if(x == par[x])return x;
	return par[x] = find(par[x]);
}

void unite(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y)return ;
	if(d[x] < d[y]){
		par[x] = y;
	}else{
		par[y] = x;
		if(d[x] == d[y])d[x]++;
	}
}

bool same(int x, int y){
	return find(x) == find(y);
}

int main(){
	int n, x, y, t;
	scanf("%d", &n);
	init(n);
	
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= n;j++){
			scanf("%d", &t);
			if(t != -1)G.push(PP(t, P(i, j)));
		}
	}
	
	int ans = 0;
	while(!G.empty()){
		PP pp = G.top();G.pop();
		P p = pp.second;
		int cost = pp.first, from = p.first, to = p.second;
		if(same(from, to))continue;
		unite(from, to);
		ans += cost;
	}
	printf("%d\n", ans);
	return 0;
}

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

				if (d[v] > M[u][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]];
		}
		return sum;
	}

	int main() {
		cin >> n;

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

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

using namespace std;
using P = pair<int, int>;

const int MAX = 100;
const int NIL = -1;

int G[MAX][MAX];

int main() {

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> G[i][j];
        }
    }

    set<int> T;
    int sum = 0;

    T.insert(0);

    while (T.size() < n) {
        priority_queue<P, vector<P>, greater<>> pq;
        for (int u : T) {
            for (int i = 0; i < n; i++) {
                if (G[u][i] != NIL && T.find(i) == T.end()) {
                    pq.push(make_pair(G[u][i], i));
                }
            }
        }
        T.insert(pq.top().second);
        sum += pq.top().first;
    }

    cout << sum << endl;
}

C++: #include<ostream>
#include<cstdio>
using namespace std;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int INFTY=(1<<21);
int n,H[100][100];
int p()
{
    int u,minv,d[100],p[100],c[100];
    for(int i=0;i<n;i++)
    {
        d[i]=INFTY;
        p[i]=-1;
    }
    d[0]=0;
    while(1)
    {
        minv=INFTY;
        u=-1;
        for(int i=0;i<n;i++)
        {
            if(minv>d[i]&&c[i]!=BLACK)
            {
                u=i;
                minv=d[i];
            }
        }
        if(u==-1) break;
        c[u]=BLACK;
        for(int v=0;v<n;v++)
        {
            if(c[v]!=BLACK&&H[u][v]!=INFTY)
            {
                if(d[v]>H[u][v])
                {
                    d[v]=H[u][v];
                    p[v]=u;
                    c[v]=GRAY;
                }
            }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(p[i]!=-1) sum+=H[i][p[i]];
    }
    return sum;
}
int main()
{
    int a;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
    {
        scanf("%d",&a);
        H[i][j]=(a==-1)?INFTY:a;
    }
    printf("%d\n",p());
    return 0;
}

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

#define int long long

struct edge{
  int u, v, cost;
  bool operator < (edge const& other){
	  return cost < other.cost;  
	}	
};

const int N = 100 + 11;
int p[N], sz[N];
vector<edge> edge_list;
vector<edge> ret;
int weight;

int get(int x){
  if (p[x] == x) return x;
  return p[x] = get(p[x]);	
}

void unite(int x, int y){
  x = get(x), y = get(y);
  if (sz[x] < sz[y]) swap(x, y);
  sz[x] += sz[y];
  p[y] = x;
}

void KruskalMST( vector<edge> &edge_list){
	sort(edge_list.begin(), edge_list.end());
	for(auto it : edge_list){
	  if (get(it.u)  != get(it.v)){
	    unite(it.u, it.v);
	    weight += it.cost;
	    ret.push_back(it);	  
     }	
  }	
}

int32_t main(){
  	int n;
  	cin >> n;
  	vector<edge> g;
  	for(int i = 0; i < n; i++){
	  p[i] = i;
	  sz[i] = 0;	
	}
  	for (int i = 0; i < n; i++){
	  for (int j = 0; j < n; j++){
		  int x;
		  cin >> x;
		  if ( x != -1) g.push_back({i, j, x});  
	  }	
	}
  KruskalMST(g);
  cout << weight << endl;
}