Go: package main

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

const R = 100000000

var sc = bufio.NewScanner(os.Stdin)

var n int
var A [101][101]int

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func prim() int {
	var u, Minimum, sum int
	var d, p, color [101]int

	for i := 0; i < n; i++ {
		d[i] = R
		p[i] = -1
		color[i] = 0
	}
	d[0] = 0

	for {
		Minimum = R
		u = -1
		for i := 0; i < n; i++ {
			if Minimum > d[i] && color[i] != 2 {
				u = i
				Minimum = d[i]
			}
		}
		if u == -1 {
			break
		}
		color[u] = 2
		for j := 0; j < n; j++ {
			if color[j] != 2 && A[u][j] != R {
				if d[j] > A[u][j] {
					d[j] = A[u][j]
					p[j] = u
					color[j] = 1
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += A[i][p[i]]
		}
	}
	return sum
}
func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			e := nextInt()
			if e == -1 {
				A[i][j] = R
			} else {
				A[i][j] = e
			}
		}
	}
	fmt.Println(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 GREY = 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; i++) {
      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<stdio.h>
#define MAX 2001
#define NIL -1
#define B -2
#define W -1
#define G 0
#include<queue>
#include<algorithm>
using namespace std;

int d[MAX];
int p[MAX];
int color[MAX];
int n;
int M[MAX][MAX];
queue <int> Q;

void Prim() {
	for (int i = 0; i < n; i++) {
		d[i] = MAX;
		p[i] = NIL;
		color[i] = W;
	}
	d[0] = 0;
	color[0] = B;
	int s = 0;
	Q.pop();
	while (!Q.empty()) {
		
		for (int i = 0; i < n; i++) {
			if (M[s][i] != -1 && M[s][i] < d[i] && color[i] != B) {
				d[i] = M[s][i];
				p[i] = s;
				color[i] = G;

			}
		}
		int mini = MAX;
		for (int j = 0; j < n; j++) {
			if (color[j] == G) {			
				if (mini > d[j]) {
					s = j;
				}
				mini= min(mini, d[j]);
			}
		}
		color[s] = B;
		Q.pop();
	}

}
int main() {
	
	cin >> n;
	int a;
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a;
			M[i][j] = a;
		}
		Q.push(i);
	}

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

C++: #include <iostream>
#include <limits>

constexpr int MAXN = 100;
int G[MAXN][MAXN];
int P[MAXN];
int D[MAXN];
int C[MAXN];

int main(){
    int n;
    std::cin >> n;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            std::cin >> G[i][j];
            if (G[i][j] == -1) {
                G[i][j] = std::numeric_limits<int>::max();
            }
        }
    }

    for (int i = 0; i < n; i++) {
        D[i] = std::numeric_limits<int>::max();
    }

    D[0] = 0;
    P[0] = -1;
    while (true) {
        int m;
        int mincost = std::numeric_limits<int>::max();
        for (int i = 0; i < n; i++) {
            if ((C[i] == 0) && (D[i] < mincost)) {
                mincost = D[i];
                m = i;
            }
        }

        if (mincost == std::numeric_limits<int>::max()) {
            break;
        }

        C[m] = 1;
        D[m] = mincost;

        for (int i = 0; i < n; i++) {
            if ((G[m][i] < std::numeric_limits<int>::max()) && (C[i] == 0) && (G[m][i] < D[i])) {
                D[i] = G[m][i];
                P[i] = m;
            }
        }
    }


    int sum = 0;
    for (int i = 0; i < n; i++) {
        if (D[i] != std::numeric_limits<int>::max()) {
            sum += D[i];
        }
    }
//    for (int i = 0; i < n; i++) {
//        std::cout << i << ": " << D[i] << std::endl;
//    }
    std::cout << sum << std::endl;

}

C++: #include<iostream>
#include<vector>
#include<algorithm>

int color(std::vector<std::vector<int> > v) {
	std::vector<int> color(v.size(), 0);
	int count = 0, color_count = 1;
	bool flag = false;
	for (int i = 1; i <= 2000; i++) {
		for (size_t j = 0; j < v.size(); j++) {
			for (size_t k = j + 1; k < v.size(); k++) {
				if (v[j][k] == i && ((color[k] == 0 || color[j] == 0) || (color[k] != color[j]))) {
					if (color[k] == 0) {
						if (color[j] != 0) {
							color[k] = color[j];
						} else {
							color[k] = color[j] = color_count;
							color_count++;
						}
					} else {
						if (color[j] == 0) {
							color[j] = color[k];
						} else {
							std::replace(color.begin(), color.end(), color[j], color[k]);
						}
					}
					count += i;
				}
			}
		}
	}
	return count;
}

int main() {
	int num;
	std::cin >> num;

	std::vector<std::vector<int> > v(num);

	for (int i = 0; i < num; i++) {
		for (int j = 0; j < num; j++) {
			int n;
			std::cin >> n;
			v[i].push_back(n);
		}
	}

	

	std::cout << color(v) << std::endl;

	return 0;
}

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

const int MAX = 100, INF = 1e9, white = 0, gray = 1, black = 2;
int G[MAX][MAX], color[MAX], d[MAX], p[MAX], n;

int prim(){
    int u, minv;
    for (int i = 0; i < n; i++)
    {
        d[i] = INF;
        p[i] = -1;
        color[i] = white;
    }
    d[0] = 0;
    while (true)
    {
        minv = INF;
        u = -1;
        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 && G[u][i] != INF)
            {
                if (d[i] > G[u][i])
                {
                    d[i] = G[u][i];
                    p[i] = u;
                    color[i] = gray;
                }
                
            }
            
        }
        
    }
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        if (p[i] != -1){
            sum += G[i][p[i]];
        }
    }
    return sum;
}

int main(){
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int v;
            cin >> v;
            G[i][j] = (v == -1) ? INF : v;
        }
    }
    cout << prim() << endl;
}