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 <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;

const int MAX = 105;
const int INF = 1000000007;
const int white = 0;
const int gray = 1;
const int black = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
  rep(i, n) {
    d[i] = INF;
    p[i] = -1;
    color[i] = white;
  }
  d[0] = 0;
  while (1) {
    minv = INF;
    u = -1;
    rep(i, n) {
      if (minv > d[i] && color[i] != black) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1) break;
    color[u] = black;
    rep(v, n) {
      if (color[v] != black && M[u][v] != INF) {
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = gray;
        }
      }
    }
  }
  int sum = 0;
  rep(i, n) {
    if (p[i] != -1) {
      sum += M[i][p[i]];
    }
  }
  return sum;
}

int main() {
  cin >> n;
  rep(i, n) {
    rep(j, n) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INF : e;
    }
  }
  cout << prim() << endl;

  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
constexpr int INF = (1 << 30) - 1;

int n;
int dist[105][105];

int prim()
{
    int d[105];
    bool flag[105] = {};
    int cnt = 0, sum = 0;
    fill_n(d, 105, INF);
    d[0] = 0;
    
    while ( true ) {
        int u = -1;
        for ( int i = 0; i < n; i++ ) {
            if ( !flag[i] && (u == -1 || d[u] > d[i] ) ) u = i;
        }
        
        flag[u] = true;
        sum += d[u];
        cnt++;
        if ( cnt == n ) break;

        for ( int i = 0; i < n; i++ ) {
            if ( !flag[i] && dist[u][i] != -1 && d[i] > dist[u][i] ) {
                d[i] = dist[u][i];
            }
        }
    }

    return ( sum );
}

int main()
{
    cin >> n;
    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; j < n; j++ ) {
            cin >> dist[i][j];
        }
    }
    cout << prim() << endl;
    return ( 0 );
}

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

#define WHITE 1
#define BLACK 2
#define GRAY 3
#define M 100
#define INFTY (1<<21)

int n,color[M],A[M][M];

int prim(){
  int d[M],p[M],u = -1,min;

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

  while(1){
    min = INFTY;
    u = -1;
    for(int i = 0;i < n;i++){
      if(color[i] != BLACK && d[i] < min){
        min = 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] != -1){
        if(A[u][i] < d[i]){
          d[i] = A[u][i];
          p[i] = u;
          color[i] = GRAY;
        }
      }
    }
  }

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

  return re;
}

int main(){

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

  cout << prim() << endl;

  return 0;
}

C++: #include <algorithm>
#include <cstdint>

using namespace std;

enum status_t {
  WHITE,
  GRAY,
  BLACK,
};

#define GRAPH_SIZE 101

static uint32_t graph[GRAPH_SIZE][GRAPH_SIZE];
static status_t state[GRAPH_SIZE];
static uint32_t shortest_path[GRAPH_SIZE];

size_t minimum_spanning_tree(size_t num)
{
  size_t minimum_cost = 0;

  for (uint32_t i = 0; i < num; i++) {
    shortest_path[i] = UINT32_MAX;
    state[i] = WHITE;
  }

  shortest_path[0] = 0;
  state[0] = GRAY;

  while (true) {
    uint32_t minimum = UINT32_MAX;
    uint32_t to = 0;
    for (uint32_t j = 0; j < num; j++) {
      if ((shortest_path[j] < minimum) && state[j] != BLACK) {
        minimum = shortest_path[j];
        to = j;
      }
    }

    if (minimum == UINT32_MAX) break;

    state[to] = BLACK;
    minimum_cost += minimum;

    for (uint32_t j = 0; j < num; j++) {
      if ((graph[to][j] != UINT32_MAX) && (state[j] != BLACK)) {
        state[j] = GRAY;
        shortest_path[j] = min(shortest_path[j], graph[to][j]);
      }
    }
  }

  return minimum_cost;
}

#include <iostream>

int32_t main()
{
  uint32_t num;
  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    for (uint32_t j = 0; j < num; j++) {
      int32_t val;
      cin >> val;
      if (val == -1) {
        graph[i][j] = UINT32_MAX;
      } else {
        graph[i][j] = val;
      }
    }
  }

  cout << minimum_spanning_tree(num) << endl;

  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <math.h>
#include <vector>
#define N 10000
using namespace std;

class edge
{
  public:
    int i, j, weigt;
    bool operator()(edge e1, edge e2)
    {
        return e1.weigt < e2.weigt;
    }
};
edge e[N];
vector<int> adj[N];
int n;
int c = 0;
int f[N] = {};
int g[N] = {};

void setGraphNum(int v, int i)
{
    g[v] = i;
    f[v]++;
    vector<int>::iterator itr = adj[v].begin();
    while (itr != adj[v].end())
    {
        if (f[*itr] !=c)
            setGraphNum(*itr, i);
        itr++;
    }
}
void CalcGraphNum()
{
    int k = 1;
    for (int i = 0; i < n; i++)
    {
        if (f[i]!=c)
            setGraphNum(i, k++);
    }
}
int main()
{
    cin >> n;
    int k = 0, tmp = 0, sum = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> tmp;
            if (j < i + 1 || tmp < 0)
                continue;
            e[k].i = i;
            e[k].j = j;
            e[k].weigt = tmp;
            k++;
        }
    }
    sort(e, e + k, edge());

    for (int i = 0; i < k; i++)
    {
        if (find(adj[e[i].i].begin(), adj[e[i].i].end(), e[i].j) != adj[e[i].i].end())
            continue;
        adj[e[i].i].push_back(e[i].j);
        adj[e[i].j].push_back(e[i].i);
        sum += e[i].weigt;
        if(++c==n-1)
            break;
        CalcGraphNum();
    }

    cout << sum << "\n";
    return 0;
}