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++: // Program Name: Minimum Spanning Tree
// Written by: by_sknight
// Date: 2019/4/27
#include <bits/stdc++.h>
using namespace std;
#define MAX 105
#define INFTY INT_MAX
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, G[MAX][MAX];

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

    d[0] = 0;

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

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int k;
            cin >> k;
            G[i][j] = (k == -1 ? INFTY : k);
        }
    }

    int sum = prim();

    cout << sum << endl;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<cstring>
#define p pair<int,int>
#define P pair<int,p>
using namespace std;

vector<P>s;
int Par[100], Rank[100];
int find(int a) {
	if (Par[a] == a)return a;
	return Par[a] = find(Par[a]);
}
void unite(int a, int b) {
	a = find(a); b = find(b);
	if (Rank[a] > Rank[b]) {
		Par[b] = a;
	}
	else if (Rank[a] < Rank[b]) {
		Par[a] = b;
	}
	else {
		Rank[a]++;
		Par[b] = a;
	}
}
signed main() {
	int a; cin >> a;
	for (int b = 0; b < a; b++)Par[b] = b;
	for (int b = 0; b < a; b++) {
		for (int c = 0; c < a; c++) {
			int d; scanf("%d", &d);
			if (d != -1 && b <= c) {
				s.push_back(P(d,p(b,c)));
			}
		}
	}
	sort(s.begin(), s.end());
	int cost = 0;
	for (P i : s) {
		if (find(i.second.first) != find(i.second.second)) {
			cost += i.first;
			unite(i.second.first, i.second.second);
		}
	}
	cout << cost << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#include <climits>
using namespace std;

int prim(vector<vector<int> >& A)
{
    int sum = 0;
    bool on[A.size()];
    for (int i = 0; i < A.size(); i++) {
        on[i] = false;
    }

    // pair<cost, node>
    priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > PQ;
    PQ.push(make_pair(0, 0));
    while (!PQ.empty()) {
        pair<int, int> edge = PQ.top();
        PQ.pop();
        if (on[edge.second]) continue;
        on[edge.second] = true;
        sum += edge.first;
        for (int i = 0; i < A.size(); i++) {
            if (on[i] || A[edge.second][i] < 0) continue;
            PQ.push(make_pair(A[edge.second][i], i));
        }
    }
    return sum;
}

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

C++: #include<iostream>
#include<vector>
#define INF (1<<21)
#define WHITE 1
#define GRAY 2
#define BLACK 3
#define neg -1
using namespace std;

int n;
int m[100][100];
vector<int> color;
vector<int> g;
vector<int> p;

int prim(){
  int mincost, u;
  
  for(int i = 0; i < n; i++){
    g[i] = INF;
    color[i] = WHITE;
    p[i] = neg;
  }

  g[0] = 0;

  while(1){
    mincost = INF;
    u = -1;

    for(int i = 0; i < n; i++){
      if(mincost > g[i] && color[i] != BLACK){
	u = i;
	mincost = g[i];
      }
    }

    if(u == -1) break;

    color[u] = BLACK;
    
    for(int i = 0; i < n; i++){
      if(color[i] != BLACK && m[u][i] != INF){
	if(g[i] > m[u][i]){
	  g[i] = m[u][i];
	  p[i] = u;
	  color[i] = GRAY;
	}
      }
    }
  }

  int sum = 0;

  for(int i = 0; i < n; i++){
    if(p[i] != neg) sum += m[i][p[i]];
  }

  return sum;
  
}

int main()
{
  cin >> n;
  color.resize(n);
  g.resize(n);
  p.resize(n);

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
    int x;
    cin >> x;

    m[i][j] = (x == -1) ? INF:x;
    
    }
  }

  cout << prim() << endl;

  return 0;

}

C++: #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;

int n;
int m[101][101];
int d[101];
int color[101];
int p[101];

int prim(){
	rep(i, n){
		d[i] = 1e5;
		color[i] = 0;
	}
	d[0] = 0;
	p[0] = -1;
	while(1){
		int mincost = 1e5;
		int u = -1;
		rep(i, n){
			if(d[i] < mincost && color[i] != -1){
				mincost = d[i];
				u = i;
			}
		}
		color[u] = -1;
		if(u == -1) break;
		rep(v, n){
			if(color[v] != -1 && m[u][v] != -1){
				if(m[u][v] < d[v]){
					d[v] = m[u][v];
					p[v] = u;
					color[v] = 1;
				}
			}
		}
	}
	int sum = 0;
	rep(i, n){
		sum += d[i];
	}
	return sum;
}

int main(){
	cin >> n;
	rep(i, n){
		rep(j, n){
			cin >> m[i][j];
		}
	}
	cout << prim() << endl;
}

C++: #include <iostream>

using namespace std;
static const int mx=107;
static const int inf=(1<<21);

int n,m[mx][mx];

int prim()
{
	int u,minv;
	int d[mx],p[mx],color[mx];
	
	for(int i=0;i<n;i++)
	{
		d[i]=inf;
		p[i]=-1;
		color[i]=0;
	}
	d[0]=0;
	
	while (1) {
		minv=inf;
		u=-1;
		for(int i=0;i<n;i++)
		{
			if(minv>d[i]&&color[i]!=2)
			{
				u=i;
				minv=d[i];
			}
		}
		if(u==-1)break;
		color[u]=2;
		for(int v=0;v<n;v++)
		{
			if(color[v]!=2&&m[u][v]!=inf)
			{
				if(d[v]>m[u][v])
				{
					d[v]=m[u][v];
					p[v]=u;
					color[v]=1;
				}
			}
		}
	}
	int sum=0;
	for(int i=0;i<n;i++)
	{
		if(p[i]!=-1)sum+=m[i][p[i]];
	}
	return sum;
}
int main(int argc, char *argv[]) {
	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)?inf:e;
		}
	}
	cout<<prim()<<endl;
	return 0;
}