Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			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] != INFTY {
				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() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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

int n;
vector<pair<int,int>> G[110];
vector<int> seen(110, false);

int prim(){
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> qu;
    int sum = 0, size = 0;

    int u=0;
    seen[u]=true;
    size=1;
    while(size<n){
        for(auto e:G[u]) if(!seen[e.second]) qu.push(e); 
        while(1){
            pair<int, int> p=qu.top(); qu.pop();
            if(!seen[p.second]){
                u = p.second;
                sum += p.first;
                seen[u] = true;
                ++size;
                break;
            }
        }
    }
    return sum;
}

int main(){
    cin >> n;
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            int w; cin >> w;
            if(w!=-1) G[i].push_back(make_pair(w, j));
        }
    }

    cout << prim() << '\n';

    return 0;
}

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

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

struct unionfind {
    vector<int> data;
    unionfind(int size) : data(size, -1) { }
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

struct edge {
    int from,to,cost;
};

vector<edge> kruscal(vector<edge> & es){
    sort(es.begin(), es.end(), [](edge a, edge b){
            return a.cost < b.cost;
        });
    unionfind uf(es.size());
    vector<edge> res;
    rep(i,es.size()){
        const edge &e = es[i];
        if(!uf.same(e.from, e.to)){
            uf.unite(e.from,e.to);
            res.push_back(e);
        }
    }
    return res;
}

int main(){
    vector<edge> es;
    int n; cin>>n;
    rep(i,n)rep(j,n){
        int c; cin>>c;
        if(c!=-1) es.push_back({i,j,c});
    }
    auto v = kruscal(es);
    int ans = 0;
    rep(i,v .size()){
        ans += v[i].cost;
    }
    cout << ans << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define REP(i, n) for (long long i = 0; i < (n); i++)
struct UnionFind {
    vector<int> parent, size;
    UnionFind(int n) {
        parent.resize(n, -1);
        size.resize(n, 1);
    }
    void unite(int x, int y) {
        if (same(x, y)) {
            return;
        }
        x = root(x);
        y = root(y);
        if (size[x] <= size[y]) {
            swap(x, y);
        }
        parent[y] = x;
        size[x] += size[y];
    }
    bool same(int x, int y) { return (root(x) == root(y)); }
    int root(int x) {
        while (parent[x] != -1) {
            x = parent[x];
        }
        return x;
    }
    int getsize(int x) { return size[root(x)]; }
};
vector<vector<P>> mst(vector<vector<P>> graph) {
    vector<pair<ll, P>> as;
    REP(i, graph.size()) {
        REP(j, graph[i].size()) { as.push_back({graph[i][j].second, {i, graph[i][j].first}}); }
    }
    sort(as.begin(), as.end());
    UnionFind uf(graph.size());
    vector<vector<P>> ans(graph.size());
    REP(i, as.size()) {
        if (!uf.same(as[i].second.first, as[i].second.second)) {
            ans[as[i].second.first].push_back({as[i].second.second, as[i].first});
            ans[as[i].second.second].push_back({as[i].second.first, as[i].first});
            uf.unite(as[i].second.second, as[i].second.first);
        }
    }
    return ans;
}
signed main() {
    int v;
    cin >> v;
    vector<vector<P>> graph(v);
    vector<vector<P>> ans(v);
    REP(i, v) {
        REP(j, v) {
            int a;
            cin >> a;
            if (a != -1) {
                graph[i].push_back({j, a});
            }
        }
    }
    ans = mst(graph);
    ll sum = 0;
    REP(i, v) {
        REP(j, ans[i].size()) { sum += ans[i][j].second; }
    }
    cout << sum / 2 << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
  static const int MAX=100;
  static const int INF=(1<<21);
  static const int WHI=0;
  static const int GRA=1;
  static const int BLA=2;
  int n,M[MAX][MAX];
  int prim(){
    int u,minv;
    int d[MAX],p[MAX],color[MAX];
    for(int i=0;i<n;i++){
      d[i]=INF;
      p[i]=-1;
      color[i]=WHI;
    }
    
    d[0]=0;
    while(1){
      minv=INF;
      u=-1;
      for(int i=0;i<n;i++){
	if(minv>d[i]&&color[i]!=BLA){
	  u=i;
	}
      }

      if(u==-1){
	break;
      }
      color[u]=BLA;

      for(int v=0;v<n;v++){
	if(color[v]!=BLA&&M[u][v]!=INF){
	  if(M[u][v]<d[v]){
	    d[v]=M[u][v];
	    p[v]=u;
	    color[v]=GRA;
	  }

	}
      }
    }

    int sum=0;
    for(int i=0;i<n;i++){
      if(p[i]!=-1){
	sum+=M[i][p[i]];
      }
    }
    return sum;
  }

  int main(){
    cin>>n;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	int e;
	cin>>e;
	if(e==-1){
	  M[i][j]=INF;
	}else{
	  M[i][j]=e;
	}
      }
    }
    
    cout<<prim()<<endl;
    return 0;
  }

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

#define NIL -1
#define MAX (1 << 24)

struct Node {
	int dis = MAX;
	bool flag = false;
	vector<int> d;
	vector<int> id;
};

void dfs(vector<Node>& u, int id)
{
	u[0].dis = 0;

	while (1)
	{
		bool fin = true;
		int mini = MAX;
		int n;
		for (int i = 0; i < u.size(); i++)
		{
			if (!u[i].flag && u[i].dis < mini) {
				mini = u[i].dis;
				n = i;
				fin = false;
			}
		}

		if (fin) return;
		u[n].flag = true;

		for (int i = 0; i < u[n].id.size(); i++)
		{
			if (u[u[n].id[i]].dis > u[n].d[i] && !u[u[n].id[i]].flag) {
				u[u[n].id[i]].dis = u[n].d[i];
			}
		}
	}
}

int main(void)
{
	int i, N, m, q = 0;
	cin >> N;
	vector<Node> u(N);
	for (i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			cin >> m;
			if (m == NIL) continue;
			u[i].d.push_back(m);
			u[i].id.push_back(j);
		}
	}

	
	dfs(u, 0);

	for (i = 0; i < N; i++) q += u[i].dis;

	cout << q << endl;

	return 0;
}