Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] >= 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A >= edges[j].A })

	sum := 0
	num := 0
	for len(edges) > 0 && num < n-1 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
		num += 1
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct edge{
    int u,v,cost;
    bool operator<(const edge& v) const {
        return cost < v.cost;
    }
};
const int INF = 1001001001;
const edge imEg = {-1,-1,INF};
vector<edge> es(10000, imEg);
int V,E;
vector<int> par;
vector<int> rnk;

void init_union_find(int n) {
    par.resize(n);
    rnk.resize(n);
    for(int i=0; i<n; i++) {
        par[i] = i;
        rnk[i] = 0;
    }
}

int find(int x) {
    if(x != par[x]) par[x] = find(par[x]);
    return par[x]; 
}

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

int kruskal() {
    init_union_find(V);
    sort(es.begin(), es.end());
    int res = 0;
    for(int i=0; i<E; i++) {
        edge e = es[i];
        if(find(e.u) != find(e.v)) {
            unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main() {
    cin >> V;
    E = 0;
    for(int i=0; i<V; i++) {
        for(int j=0; j<V; j++) {
            int w;
            cin >> w;
            if(w != -1) {
                struct edge e = {i,j,w};
                es[E] = e;
                E++;
            }
        }
    }
    cout << kruskal() << endl;
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

struct Edge{
	int dist,cost;
	Edge(){}
	Edge(int dist,int cost):dist(dist),cost(cost){}
};
	bool operator<(const Edge& e1, const Edge& e2){
		return e1.cost > e2.cost;
	}


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
	int n;cin>>n;
	vector<vector<Edge> > edge(n,vector<Edge>());
	REP(i,n){
		REP(j,n){
			int c;
			cin>>c;
			if(c == -1)continue;
			edge[i].push_back(Edge(j,c));
		}
	}
	priority_queue<Edge,vector<Edge>,less<Edge> > que;
	que.push(Edge(0,0));//from 0
	int ct = 0;
	int ans = 0;
	vector<bool> primed(n,false);
	while(ct < n){
		Edge e = que.top();
		que.pop();
		if(primed[e.dist])continue;
		primed[e.dist] = true;
		ct++;
		ans += e.cost;
		for(auto e2 : edge[e.dist]){
			que.push(e2);
		}
	}
	cout << ans << endl;
  return 0;
}

C++: /*
* @Author: Samson
* @Date:   2018-06-12 18:24:44
* @Last Modified by:   Samson
* @Last Modified time: 2018-06-13 12:52:45
*/
//   @URL : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
#include<bits/stdc++.h>
#include<algorithm>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e2+10;

int p[MAXN],color[MAXN],a[MAXN][MAXN],t,n,d[MAXN];

int prim()
{
	int minv,u,v;
	memset(p,-1,sizeof p);
	memset(d,INF,sizeof d);

	d[2] = 0;
	int ans = 0;

	while(1)
	{
		minv = INF;
		u = -1;
		for(int i = 1; i <= n; ++i)
		{
			if(minv > d[i] && !color[i])
				minv = d[i],u = i;
		}
		if(u == -1)	break; //空集
		color[u] = 1; //从集合中删除
		for(int v = 1; v <= n; ++v)
		{
			if(!color[v] && a[u][v] != INF)
			{
				if(d[v] > a[u][v])
				{
					d[v] = a[u][v];
					p[v] = u;
				}
			}
		}
	}

	for(int i = 1; i <= n; ++i)
		if(p[i] != -1)
			ans += a[p[i]][i];
	return ans;
}

int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	cin>>n;
	for(int i = 1; i <= n; ++i)
	{
		for(int j = 1; j <= n; ++j)
		{
			cin>>t;
			a[i][j] = t==-1?INF:t;
		}
	}
	cout<<prim()<<'\n';
	return 0;
}

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

#define MAX_V 10000
#define INF 1e9

typedef pair<int, int> pii; // <cost, to>

int cost[MAX_V][MAX_V]; // cost[u][v]??????e=(u,v)????????????(?????¨???????????´??????INF)
bool used[MAX_V]; // ??????i???X????????????????????????
int V;

int prim() {
  for (int i = 0; i < V; ++i) {
    used[i] = false;
  }
  std::priority_queue<pii, vector<pii>, greater<pii> > que;
  que.push(pii(0, 0));
  int res = 0;

  while (!que.empty()) {
    int cst = que.top().first, v = que.top().second;
    que.pop();
    if (used[v])
      continue;
    used[v] = true;
    res += cst;
    for (int u = 0; u < V; u++)
      que.push(pii(cost[v][u], u));
  }

  return res;
}

int main(){
    cin >> V;
    for(int i=0;i<V;++i){
        for(int j=0;j<V;++j){
            int a;
            cin >> a;
            cost[i][j] = (a == -1 ? INF : a);
        }
    }
    cout << prim() << endl;

    return 0;
}

C++: #include <iostream>
#include <utility>
#include <vector>
#include <algorithm>


using namespace std;

int P[110];

void init(int N){
    for(int i = 0; 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; 
    cin >> n; 
    int w;
    init(n);
    vector< pair<int, pair<int, int> > > edges;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            cin >> w;
            if (i > j && w != -1) edges.push_back(make_pair(w, make_pair(i, j)));
        }
    }
    sort(edges.begin(),edges.end());
    int sum = 0;
    while(!edges.empty()){
        pair<int, pair<int, int> > edge = edges[0];
        edges.erase(edges.begin());
        int cost = edge.first;
        int src = edge.second.first;
        int dst = edge.second.second;
        //cout << cost << " " << src << " " << dst << endl;
        if (!is_same_set(src, dst)){
            //cout << "unite " << src << " " <<  dst << " cost = " << cost << endl;
            unite(src, dst);
            sum += cost;
        }
    }
    cout << sum << endl;
}