Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

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