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++: #include <bits/stdc++.h>
using namespace std;

struct UF{
   vector<int> par;
   vector<int> sz;
   UF(){}
   UF(int n){
       par.resize(n);
       sz.resize(n, 1);
       for(int i = 0; i < n; i++) par[i] = i;
   }
   int find(int x){
       if(x == par[x]) return x;
       return par[x] = find(par[x]);
   }
   void unite(int x, int y){
       x = find(x); y = find(y);
       if(x == y) return;
       if(sz[x] < sz[y]) swap(x, y);
       sz[x] += sz[y];
       par[y] = x;
   }
   bool same(int x, int y){
       return find(x) == find(y);
   }
};

struct edge{
    int u, v, cost;
    bool operator<(const edge &e) const{
        return cost < e.cost;
    }
};

int n, m;
vector<edge> es; 

int kruskal(){
    int res = 0;    
    UF uf(n);
    sort(es.begin(), es.end());
    for(int i = 0; i < m; i++){
        edge e = es[i];
        if(!uf.same(e.u, e.v)){
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main(){
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int u, v, w;
        cin >> u >> v >> w;
        //u--; v--;
        es.push_back({u, v, w});
    }
    cout << kruskal() << endl;
}

/* verified
https://onlinejudge.u-aizu.ac.jp/#/courses/library/5/GRL/2/GRL_2_A
*/

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

typedef long long ll;

struct Edge {
    ll s,t;
    ll w;
    bool operator < (const Edge& tgt) const {
	return w > tgt.w;
    }
    void dump(){
	cout << s << " --> " << t << " : " << w << endl; 
    }
};

ll Prim(vector<vector<Edge>>& G) {
    const ll V = G.size();
    priority_queue<Edge> edges;
    
    vector<int> used(V,0);
    //??????0??????????????????
    used[0] = 1;
    for(auto& e : G[0]) edges.push(e);

    ll ans = 0;
    while( !edges.empty() ) {
	Edge e = edges.top(); edges.pop();
	if( used[e.t] == 1 ) continue;
	used[e.t] = 1;
	ans += e.w;
	assert(used[e.s == 1]);
	for(auto& nxt : G[e.t]) {
	    if( used[nxt.t] == 1 ) continue;
	    edges.push(nxt);
	}
    }
    return ans;
}


int main () {

    ll V;
    cin >> V;
    vector<vector<Edge>> G(V);

    for(int i=0; i<V; i++) {
	for(int j=0; j<V; j++) {
	    ll w; cin >> w;
	    if( w != -1 and w != 0) {
		G[i].push_back( Edge{i,j,w} );
		G[j].push_back( Edge{j,i,w} );
//		cout << i << " <--> " << j << " : " << w << endl; 
	    }
	}
    }

    ll ans = Prim(G);
    cout << ans << endl;
        
    return 0;
}

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



class UnionFind{
    int N;
    vector<int> par;
    vector<int> rank;
    public:
    UnionFind(int N):N(N),par(N),rank(N,0){iota(par.begin(),par.end(),0);}
    int find(int x);
    void unite(int x,int y);
    bool same(int x,int y){return find(x)==find(y);}
};

int UnionFind::find(int x){
    if(par[x]==x) return x;
    return par[x]=find(par[x]);
}
void UnionFind::unite(int x,int y){
    x=find(x),y=find(y);
    if(x==y) return;
    if(rank[x]>=rank[y]){
        if(rank[x]==rank[y]) rank[x]++;
        par[y]=x;
    }else{
        par[x]=y;
    }
}

using Weight=long long;
struct Edge{
    int from,to;
    Weight weight;
};
bool operator<(const Edge& lhs,const Edge& rhs){
    return lhs.weight<rhs.weight;
}

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


Edges MinimumSpanningTree(int n,Edges edges){
    Edges res;
    UnionFind u(n);
    for(int i=0;i<edges.size();i++){
        if(!u.same(edges[i].from,edges[i].to)){
            res.push_back(edges[i]);
            u.unite(edges[i].from,edges[i].to);
        }
    }
    return res;
}

Edges MinimumSpanningTree(const Graph& g){
    Edges edges;
    for(int i=0;i<g.size();i++){
        copy(g[i].begin(),g[i].end(),back_inserter(edges));
    }
    auto res=MinimumSpanningTree(g.size(),edges);
    return res;
}

int main(){
    int n;
    cin>>n;
    Graph g(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            Weight c;
            cin>>c;
            if(i==j) continue;
            g[i].push_back({i,j,c});
        }
    }
    auto mst=MinimumSpanningTree(g);
    cout<<accumulate(mst.begin(),mst.end(),Weight(0),[](const Weight& lhs,const Edge& rhs){return lhs+rhs.weight;})<<endl;
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int main()
{
    int n, inf = 10000000;
    scanf("%d", &n);
    vector<vector<int>> a(n, vector<int>(n));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0, k; j < n; j++)
        {
            scanf("%d", &k);
            a[i][j] = k == -1 ? inf : k;
        }
    }
    
    vector<int> cost(n, inf);
    vector<bool> vis(n, false);
    cost[0] = 0;
    for (int mi = inf, u = 0; ; mi = inf)
    {
        for (int i = 0; i < n; i++)
        {
            if (!vis[i])
            {
                if (cost[i] < mi)
                {
                    mi = cost[i];
                    u = i;
                }
            }
        }
        if (mi == inf)
        {
            break;
        }
        vis[u] = true;
        for (int i = 0; i < n; i++)
        {
            if (!vis[i] && a[u][i] != -1)
            {
                cost[i] = min(cost[i], a[u][i]);
            }
        }
    }
    printf("%d\n", accumulate(cost.begin(), cost.end(), 0));
}

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

class Edge {
public:
	int v1, v2, d;
};

int n, k=0, V[100];
Edge E[5000];

void Sort() {
	for (int i = 1; i < k; i++) {
		Edge v = E[i];
		int j = i - 1;
		while (j >= 0 && E[j].d > v.d) {
			E[j + 1] = E[j];
			j--;
		}
		E[j + 1] = v;
	}
}

int Kruskal() {
	int m = 0;
	for (int i = 0; i < k; i++) {
		Edge e = E[i];
		if (V[e.v1] != V[e.v2]) {
			int v = V[e.v1], u = V[e.v2]; m += e.d;
			for (int j = 0; j < n; j++) {
				if (V[j] == u) V[j] = v;
			}
		}
	}
	return m;
}

int main() {
	int g;
	cin >> n;
	for (int i = 0; i < n; i++) {
		V[i] = i;
		for (int j = 0; j < n; j++) {
			cin >> g;
			if (i < j && g > -1) {
				E[k].v1 = i; E[k].v2 = j; E[k].d = g; k++;
			}
		}
	}

	Sort();

	cout << Kruskal() << endl;
}