Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

C++: #include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 105;

int m[105][105];
int n,e;

int prim(int s)
{
    int minv,cnt[MAXN],k,ans = 0;
    for(int i = 1; i <= n; ++i)
        cnt[i] = m[s][i];
    cnt[s] = 0;
    for(int i = 1; i < n; ++i)
    {
        minv = INF;
        for(int j = 1; j <= n; ++j)
        {
            if(cnt[j] && cnt[j]<minv)
                minv = cnt[j],k = j;
        }
        cnt[k] = 0;
        ans += minv;
        for(int j = 1; j <= n; ++j)
            if(m[k][j] < cnt[j])
                cnt[j] = m[k][j];
    }
    return ans;
}

int main(void)
{
    cin>>n;
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            cin>>e;
            m[i][j] = e==-1?INF:e;
        }
    }
    cout<<prim(1)<<'\n';
    return 0;
}

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