Go: package main

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

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

const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())
			if c != -1 {
				M[i][j] = c
			} else {
				M[i][j] = math.MaxInt64
			}

		}
	}
	prim()
}

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

	for i := 0; i < n; i++ {
		d[i] = math.MaxInt64
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		minv = math.MaxInt64
		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] != math.MaxInt64 {
				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]]
		}
	}
	fmt.Println(sum)

}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<numeric>
#include<stdio.h>

using std::cout;
using std::cin;
using std::setw;
using std::setfill;
using std::string;
using std::pair;
using std::make_pair;
using std::vector;
using std::queue;
using std::deque;
using std::priority_queue;
using std::next_permutation;
using std::iota;
using std::sort;
using std::greater;
using std::max;
using std::min;

#define INF 999999999
#define MOD 1000000007

#define EVEL 1

#ifndef EVEL
#define dbug(X) std::cout << #X << ":" <<X<<" " ;
#define dbugf(s) std::cout << s << " ";
#define dbugln std::cout<<"\n";
#else
#define dbug(X) {}
#define dbugf(s) {}
#define dbugln {}
#endif

/*const int M = 10000;

class Matrix{
	public:
	int n;//行
	int m;//列
	vector<int> data;
	Matrix(int a, int b, const vector<int> &v){
		n = a;
		m = b;
		data.resize(n*m);
		for(int i = 0; i < n*m; i++){
			data[i] = v[i];
		}
	}

	int show(){
		cout << this->n << "x" << this->m << "Matrix\n"; 
		for(int i = 0; i < this->n; i++){
			for(int j = 0; j < this->m; j++){
				cout << data[i * this->m + j];
				if(j == this->m-1) cout << "\n";
				else cout << " ";
			}
		}
		return 0;
	}
	Matrix operator * (Matrix obj){
		vector<int> rt;
		int a=0;
		for(int i = 0; i < this->n; i++){
			for(int j = 0; j < obj.m; j++){

				for(int k = 0; k < this->m; k++){
					//a += this->data[i * this->m + k] * obj.data[k * obj.m + j];
					a = (a%M + ((this->data[i * this->m + k]%M)*(obj.data[k * obj.m + j]%M))%M)%M;
				}
				rt.push_back(a);
				a = 0;

			}
		}
		Matrix RT(this->n, obj.m, rt);
		return RT;
	}

	Matrix expt(long long int p){
		if(p == 0){
			vector<int> v{1, 0, 0, 1};
			Matrix RT(2, 2, v);
			return RT;
		}else if(p == 1) return *this;
		else if(p%2 == 1){
			Matrix T = this->expt(p-1);
			return *this * T;
		}else{
			Matrix T = this->expt(p/2);
			return T*T;
		}
	}
};*/

class UF{
	vector<int> oya;

	public:
	UF(int s){
		oya.resize(s);
		for(int i = 0; i < s; i++) oya[i] = i;
	}

	int root(int x){
		vector<int> path;
		while(oya[x] != x){
			path.push_back(x);
			x = oya[x];
		}
		for(int i = 0; i < path.size(); i++){
			oya[ path[i] ] = x;
		}
		return x;
	}

	void unite(int x, int y){
		//x <= s, y <= s が保証されているものとする
		int ox = root(x), oy = root(y);
		oya[oy] = ox;
	}

	bool same(int x, int y){
		int ox = root(x), oy = root(y);
		return (ox == oy);
	}
};

int n, vsum=0;

struct path{
	int value;
	int start;
	int end;
};

vector<path> ps;

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

	cin >> n;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			int a;
			cin >> a;
			if(a >= 0 && i < j){
				path p;
				p.value = a;
				p.start = i;
				p.end = j;
				ps.push_back(p);
			}
		}
	}

	UF uf(n);
	sort(ps.begin(), ps.end(), [](path a, path b){return (a.value < b.value);});
	
	for(auto itr = ps.begin(); itr != ps.end(); itr++){
		if(!uf.same(itr->start, itr->end)){
			uf.unite(itr->start, itr->end);
			vsum+=itr->value;
		}
		dbug(vsum)dbug(itr->value)dbug(itr->start)dbug(itr->end)dbugln;
	}

	cout << vsum << "\n";


	return 0;
}

C++: //compiledef
#include <bits/stdc++.h>
#define rep(i, n)for(i=0;i<(n);i++)
#define per(i, n) for(i=(n);i>0;i--)
#define repx(i, n, x) for(i=(x);i<(n);i++)
#define xper(i, n, x) for(i=(n);i>(x);i--)
#define pback push_back

//c++def
using namespace std;
typedef long long int ll;
const long long int MOD = 1e9+7;
const long long int MOD2 = 998244353;
int w[200][200];
int prim_mst(int n){
	int i;
	int j;
	int cost = 0;
	int d[200];
	int pi[200];
	int color[200];
	rep(i, n){
		d[i]= 2e9;
		pi[i]=-1;
		color[i] = 0;
	}
	d[0] = 0;
	while(29091101){
		int mincost = 2e9;
		rep(i, n){
			if(color[i] != 2 && d[i] < mincost){
				mincost = d[i];
				j = i;
			}
		}
		if(mincost == 2e9)break;
		color[j] = 2;
		for(int v=0; v<n;v++){
			if(color[v] != 2 && w[j][v] < d[v]){
				pi[v] = j;
				d[v] = w[j][v];
			}
		}
	}
	rep(i, n){
		if(pi[i] != -1) cost += w[i][pi[i]];
	}
	return cost;
}

int main(){
	int n;
	int i, j;
	int tmp;
	cin >> n;
	rep(i, n){
		rep(j, n){
			cin >> tmp;
			if(tmp == -1) w[i][j] = 2e9;
			else w[i][j] = tmp;
		}
	}
	cout << prim_mst(n) << endl;
}

C++: //kruskal tree
#include <iostream>
#include <algorithm>
using namespace std;
#define M 999
int parent[M],a[M],b[M];
pair<int,int>node[M];
int root(int a){return parent[a]==a?a:parent[a]=root(parent[a]);}
int unite(int a,int b){
	int x=root(a),y=root(b);
	if(x==y)return 0;
	parent[x]=y;
	return 1;
}
int main(){
	int i,j,s,n,m,x;
	cin>>n;
		for(m=i=0;i<n;i++)for(j=0;j<n;j++)if(cin>>x,~x)a[m]=i,b[m]=j,node[m].first=x,node[m].second=m++;
		sort(node,node+m);
		for(i=0;i<n;i++)parent[i]=i;
		for(s=i=0;i<m;i++)if(unite(a[node[i].second],b[node[i].second]))s+=node[i].first;
	cout<<s<<endl;
}

C++: // Graph II - Minimum Spanning Tree
// 問題URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A&lang=jp
//
// 使うライブラリも多く、難しく感じた...
#include <iostream>
#include <algorithm>

using namespace std;

int n, buff, cost[101][101]={}, P[101], M = 0, ans = 0;
pair<int, int> edges[10010], vertices[10010];

void init(int n){
  for(int i = 0; i < n; ++i){
    P[i] = i;
  }
}

int root(int x){
  if(P[x]==x) return x;
  P[x] = root(P[x]);
  return P[x];
}

bool is_same_set(int x, int y){
  return root(x) == root(y);
}

void unite(int x, int y){
  P[root(x)] = root(y);
}

int main(){
  cin >> n;

  for(int row = 0; row < n; ++row){
    for(int col = 0; col < n; ++col){
      cin >> buff;
      cost[row][col] = buff; //隣接行列の読込
    }
  }

  for(int row = 0; row < n; ++row){
    for(int col = 0; col < row; ++col){
      if(cost[row][col]>0){
        edges[M].first = cost[row][col];
        edges[M].second = M;
        cout << M << ": (" << row << ", " << col << ") = " << edges[M].first << endl;
        vertices[M].first = row, vertices[M].second = col;
        M++; //辺の数を管理、idとして利用
      }
    }
  }

  init(n);

  sort(edges, edges+M); //重みの小さい順に整列

  for(int i = 0; i <= M; ++i){
    int index = edges[i].second; //辺のidを取り出す
    if(!is_same_set(vertices[index].first, vertices[index].second)){ //辺の両端が繋がっていなければ
      unite(vertices[index].first, vertices[index].second); //両端を繋げる
      cout << "unite " << vertices[index].first << " and " << vertices[index].second << endl;
      ans += edges[i].first; //コストを加える
    }
  }
  cout << ans << endl;
}