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 GRAY = 1;  // ?¬??????¢?´¢????±?
static const int BLACK = 2; // ??¢?´¢??????

int n, M[MAX][MAX];  // ??°??????????????????????????¨????????£??\??????

int prim() {
    int u, minv; // u??????
    int d[MAX], p[MAX], color[MAX]; // d:??¢?´¢??????????±?, p:MST??????????????????v??????

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

    d[0] = 0; // MST?????¢?´¢????§?????????????????????°???

    while (1){
	    minv = INFTY;
	    u = -1;
	    for(int i = 0; i < n; i++){
		    // ??¢?´¢????±?????????§??????????°??????????????????§??????????????¢?´¢???????????????????????????	
		    if(minv > d[i] && color[i] != BLACK){
			    u = i; // MST?????????????????????????±???????
			    minv = d[i];
		    }
	    }
	    if(u == -1) break; //??¨??????????±??????¢?´¢????????¨???????????¢?´¢??????????????? 
	    color[u] = BLACK; // ??¢?´¢???????????????
	    for(int v = 0; v < n; v++){
		   //?????¢?´¢????????§?????????u??¨?????????????????£???????????????
		   //  ?¬??????¢?´¢????±???¨????????????????????????????????????
		   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]]; // MST???????????????????????¨???
    }

    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>
using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int BLACK=2;
static const int GRAY=1;
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(void){
    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 <algorithm>
#include <climits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

int n;
vector<int> g[101];
vector<int> d(101);
vector<int> p(101);
vector<int> vis(101);

void prim(){
    for (int i = 0; i < n; ++i) {
        vis[i] = 0;
        d[i] = INT_MAX;
        p[i] = -1;
    }

    d[0] = 0;
    while(true) {
        int mincost = INT_MAX;
        int next = -1;
        for (int i = 0; i < n; ++i) {
            if(vis[i] == 0 && d[i] < mincost) {
                mincost = d[i];
                next = i;
            }
        }
        if(next == -1) break;
        vis[next] = 1;

        for (int j = 0; j < n; ++j) {
            if(g[next][j] < d[j] && vis[j] != 1 && g[next][j] != -1) {
                p[j] = next;
                d[j] = g[next][j];
            }
        }
    }
}

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

    prim();

    long cst=0;
    for (int k = 0; k < n; ++k) {
        cst += d[k];
    }
    cout << cst << endl;

    return 0;
}

C++: #include <stdio.h>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 0;
static const int BLACK = 0;

int n, M[MAX][MAX];

int prim() {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
    }
    d[0] = 0;
    p[0] = -1;
    
    while(1) {
        int u;
        int mincost = INFTY;
        for (int 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 (int v=0; v<=n; v++) {
            if (color[v] != BLACK && M[u][v] != -1) {
                if(M[u][v] < d[v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    
    int sum=0;
    for (int i=0; i<n; i++) {
        sum += d[i];
    }
    
    return sum;
}

int main() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            scanf("%d", &M[i][j]);
        }
    }
    
    int sum = prim();
    printf("%d\n", sum);
    
    return 0;
}

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

int n; // number of vertices 
int P[2020], A[2020], B[2020], C[2020];
pair<int, int> D[2020]; // Weights and coordinates 

int root(int a) { // find the root of a
  if (P[a] == a) return a; // a is the root
  return  (P[a] = root(P[a])); // find root of a, make it a's parent
}

bool is_same_set(int a, int b) { // a and b are in the same group?
  return root(a) == root(b);
}

void unite(int a, int b) { // put a and b in same group
  P[root(a)] = root(b);
}


int main() {
  cin >> n; // the number of vertices
  for (int i=0; i<n; ++i) {
    P[i] = i; // Initialize vertices to disjoint sets
    for (int j=0; j<n; ++j) {
      A[(n * i) + j] = i;
      B[(n * i) + j] = j;
      cin >> C[(n * i) + j];
    }
  }
  for (int i=0; i < n*n; i++) {
    D[i].first = C[i];
    D[i].second = i;
  }
  sort(D, D+n*n);
  int sum = 0;

  for (int i=0; i<n*n; ++i) {
    if (D[i].first >= 0) { // Assuming the weight is not -1
      if (!is_same_set(A[D[i].second], B[D[i].second])) {
	unite(A[D[i].second], B[D[i].second]);
	sum += D[i].first;
      }
    }
  }
  cout << sum << endl;
}