Go: package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

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;
}