Go: package main

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

const (
	MAX = 100
	INF = 1 << 21
	WHITE = 0
	GRAY = 1
	BLACK = 2
)

var (
	n int
	M [MAX][MAX]int
)

func prim() int {
	var u, minv int
	var d [MAX]int
	var p [MAX]int
	var color [MAX]int

	for i := 0; i < n; i++ {
		d[i] = INF
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0
	for {
		minv = INF
		u = -1
		for 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 v := 0; v < n; v++ {
			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
				}
			}
		}
	}
	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			scanner.Scan()
			e, _ := strconv.Atoi(scanner.Text())
			if e == -1 {
				M[i][j] = INF
			} else {
				M[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

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

#define NIL -1
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 1 << 21

int prim(vector<vector<int> > &graph, int n){

    vector<int> parent(n, NIL);
    vector<int> cost(n, INF);
    vector<int> color(n, WHITE);

    cost[0] = 0;
    while(1){
        int minv = INF;
        int u = NIL;
        rep(i, n){
            if(cost[i] < minv && color[i] != BLACK){
                minv = cost[i];
                u = i;
            }
        }
        if(u == NIL) break;

        color[u] = BLACK;
        rep(j, n){
            if(color[j] != BLACK && graph[u][j] != NIL){
                if(cost[j] > graph[u][j]){
                    cost[j] = graph[u][j];
                    parent[j] = u;
                    color[j] = GRAY;
                }
            }
        }
    }

    int sum = 0;
    rep(i, n){
        if(parent[i] != NIL){
            sum += graph[i][parent[i]];
        }
    }
    return sum;
}

int main() {

    int n;
    cin >> n;
    vector<vector<int> > graph(n, vector<int>(n));
    rep(i, n){
        rep(j, n){
            cin >> graph[i][j];
        }
    }

    cout << prim(graph, n) << endl;

    return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

template <class T>
using gp_queue=priority_queue<T, vector<T>, less<T>>;

using Weight=int;
using DArray=vector<Weight>;
using DMatrix=vector<DArray>;

static const Weight INF=1<<29;

struct Edge {
    size_t src, dst;
    Weight weight;
    Edge() {}
    Edge(size_t src, size_t dst, Weight weight=1):
        src(src), dst(dst), weight(weight)
    {}
};

bool operator<(const Edge &e, const Edge &f) {
    if (e.weight != f.weight) {
        return e.weight > f.weight;
    } else {
        return e.src!=f.src? e.src<f.src : e.dst<f.dst;
    }
}

using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w=1) {
    // non-directed
    g[s].push_back(Edge(s, d, w));
    g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w=1) {
    // directed
    g[s].push_back(Edge(s, d, w));
}

pair<Weight, Edges> minimum_spanning_tree(const Graph &g, size_t s=0) {
    /* Prim */
    size_t V=g.size();
    Edges T;
    Weight total=0;

    vector<bool> visited(V);
    gp_queue<Edge> q; q.push(Edge(-1, s, 0));
    while (!q.empty()) {
        Edge e=q.top(); q.pop();
        if (visited[e.dst]) continue;

        T.push_back(e);
        total += e.weight;
        visited[e.dst] = true;
        for (const Edge &f: g[e.dst])
            if (!visited[f.dst])
                q.push(f);
    }

    return make_pair(total, T);
}

int main() {
    size_t n;
    scanf("%zu", &n);

    Graph g(n);
    for (size_t i=0; i<n; ++i)
        for (size_t j=0; j<n; ++j) {
            Weight a;
            scanf("%d", &a);

            if (a > -1)
                connect(g, i, j, a);
        }

    printf("%d\n", minimum_spanning_tree(g).first);
    return 0;
}

C++: #include <iostream>
#include<cstdio>
using namespace std;
const int INF = 20001;
static int G[100][100];

int prim(int n){
    
    const int BLACK = 2,GRAY = 1,WHITE =  0;
    int color[n],d[n];
    for(int i = 0;i < n;++i){
        d[i] = INF;
        color[i] = WHITE;
    }
    d[0] = 0;
    int min = INF,u;
    while(1){
        int min = INF;
        for(int i=0;i < n;++i){
            if(color[i] != BLACK && d[i] < min){
                min = d[i];
                u = i;
            }
        }
        if(min == INF) 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];
                    color[i] = GRAY;
                }
            }
        }
        //for(int i = 0;i < n;++i) printf("%d ",d[i]);
    }
    int sum = 0;
    for(int i = 0;i < n;i++) sum += d[i];
    return sum;
}

int main(void){
    int n;
    scanf("%d",&n);
    for(int i = 0;i < n;++i){
        for(int j = 0;j < n;++j){
            scanf("%d",&G[i][j]);
            G[i][j] = (G[i][j] == -1) ? INF:G[i][j];
        }
    }

    printf("%d\n",prim(n));
    
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int P[1010];

void init(int N)
{
    for(int i = 1; i <= N; ++i)
    {
        P[i] = i;
    }
}
 
int root(int a)
{
    if(P[a] == a)
    {
        return a;
    }
    return (P[a] = root(P[a]));
}
 
bool is_same_set(int a, int b)
{
    return root(a) == root(b);
}
 
void unite(int a, int b)
{
    P[root(a)] = root(b);
}

int main()
{
	int n = 0;
	cin >> n; 
	int cost[5000];
	int src[5000];
	int dst[5000];
	int A[100 + 10][100 + 10];
	int M = 0;
	vector<pair<int,int>> edges;

	for(int r = 0; r < n; ++r)
	{
		for(int c = 0; c < n; ++c)
		{
			cin >> A[r][c];
		}
	}
	
	for (int r = 0; r < n; ++r)
	{
		for(int c = r + 1; c < n; ++c)
		{
			if(A[r][c] > -1)
			{
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				edges.push_back(make_pair(cost[M],M));
				M += 1;
			}
		}
	}
	sort(edges.begin(),edges.end());
	int sum = 0;
	init(n);
	for(int i = 0; i < M; ++i)
	{
		if(is_same_set(src[edges[i].second], dst[edges[i].second]) == 1) continue;
			unite(dst[edges[i].second],src[edges[i].second]);
			sum += edges[i].first;
	}
	cout << sum << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define inf 1e9+7
typedef long long ll;

int n;
vector<vector<int> > cost;

void solve(int s){
    vector<bool> flg(n,false);
    flg[s]=true;
    vector<int> vc;
    vc.push_back(s);


    int sum=0;
    int mi,next;
    while(1){
        mi=inf;
        next=-1;
        for(int i=0;i<vc.size();i++){
            for(int j=0;j<n;j++){
                if(flg[j]||cost[vc[i]][j]<0)continue;
                if(mi>cost[vc[i]][j]){
                    mi=cost[vc[i]][j];
                    next=j;
                }
            }
        }
        if(next==-1)break;
        else{
            sum+=mi;
            flg[next]=true;
            vc.push_back(next);
        }
    }
    cout << sum << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    cost.resize(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int a;
            cin >> a;
            cost[i].push_back(a);

        }
    }

    solve(0);
    return 0;
}