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<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

struct Node{
  int color, dist, parent;
};

int n, M[MAX][MAX];
Node node[MAX];

void Printnodes(){
  string col[3] = {"WHITE", "GRAY ", "BLACK"};
  for(int i = 0; i < n; i++){
    cout << "nodeID : " << i ;
    cout << " color : " << col[node[i].color];
    cout << " parent : " << node[i].parent;
    cout << " dist : " << node[i].dist << endl;
  }
  cout << endl;
}

int main(){
  cin >> n;

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> M[i][j];
    }
    node[i].color = WHITE;
    node[i].dist = INFTY;
    node[i].parent = -1;
  }

  int p = 0, totaldist = 0;;
  node[p].color = BLACK;
  node[p].dist = 0;

  while(true){
    //Printnodes();

    for(int i = 0; i < n; i++){
      if(M[p][i] > 0 && node[i].color != BLACK){
        node[i].dist = node[p].dist + M[p][i];
        node[i].parent = p;
        node[i].color = GRAY;
      }
    }

    int min_d = INFTY;
    p = -1;
    for(int i = 0; i < n; i++){
      if(node[i].dist < min_d && node[i].color != BLACK){
        min_d = node[i].dist;
        p = i;
      }
    }

    if(p == -1)
      break;

    node[p].color = BLACK;
    totaldist = node[p].dist;
    //cout << "node " << p << " turns to BLACK!" << endl;
    //cout << "Total distance is " << totaldist << endl;
  }

  cout << totaldist << endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define IN(v) int v;cin>>v;
#define OUT(v) cout<<v<<endl

int main() {
  IN(N);

  vvi A(N, vi(N, -1));
  REP(i, N) {
    REP(j, N) {
      IN(a);
      A[i][j] = a;
    }
  }
  priority_queue<pii, vector<pii>, greater<pii>> edge;
  REP(i, N) {
    if (A[0][i] != -1)
      edge.push(make_pair(A[0][i], i));
  }
  vector<bool> visited(N, false);
  visited[0] = true;
  int ans = 0;
  while (!edge.empty()) {
    pii p = edge.top(); edge.pop();
    if (visited[p.second])
      continue;
    visited[p.second] = true;
    ans += p.first;
    REP(i, N) {
      if (A[p.second][i] != -1 && !visited[i])
        edge.push(make_pair(A[p.second][i], i));
    }
  }
  OUT(ans);

  return 0;
}

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

static const int MAX=100;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n,M[MAX][MAX];

int prime(){
  int u,mincost;
  int d[MAX],p[MAX],color[MAX];

  for(int i=0;i<n;i++){
    d[i]=INT_MAX;
    p[i]=-1;
    color[i]=WHITE;
  }

  d[0]=0;

  while(1){
    mincost=INT_MAX;
    u=-1;
    for(int i=0;i<n;i++){
      if(color[i]!=BLACK&&d[i]<mincost){
	mincost=d[i];
	u=i;
      }
    }
 
    if(mincost==INT_MAX) break;
    
    color[u]=BLACK;

    for(int i=0;i<n;i++){
      if(color[i]!=BLACK&&M[u][i]!=INT_MAX){
	if(M[u][i]<d[i]){
	  d[i]=M[u][i];
	  p[i]=u;
	  color[i]=GRAY;
	}
      }
    }
  }

  int sum=0;
  for(int i=0;i<n;i++) sum+=d[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]=INT_MAX;
      else M[i][j]=e;
    }
  }
  cout<<prime()<<endl; 
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

/* UnionFind：素集合系管理の構造体(union by rank)
	isSame(x, y): x と y が同じ集合にいるか。 計算量はならし O(α(n))
	unite(x, y): x と y を同じ集合にする。計算量はならし O(α(n))
*/
struct UnionFind {  // The range of node number is u 0 v n-1
	vector<int> rank, parents;
	UnionFind() {}
	UnionFind(int n) {  // make n trees.
		rank.resize(n, 0);
		parents.resize(n, 0);
		for (int i = 0; i < n; i++) {
			makeTree(i);
		}
	}
	void makeTree(int x) {
		parents[x] = x;  // the parent of x is x
		rank[x] = 0;
	}
	bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
	void unite(int x, int y) {
		x = findRoot(x);
		y = findRoot(y);
		if (rank[x] > rank[y]) {
			parents[y] = x;
		}
		else {
			parents[x] = y;
			if (rank[x] == rank[y]) {
				rank[y]++;
			}
		}
	}
	int findRoot(int x) {
		if (x != parents[x]) parents[x] = findRoot(parents[x]);
		return parents[x];
	}
};

// 辺の定義
struct Edge {
	long long u;
	long long v;
	long long cost;
};

bool comp_e(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; } // 辺を直接比較するための関数

/* Kruskal :クラスカル法で minimum spanning tree を求める構造体
	入力: 辺のvector, 頂点数V
	最小全域木の重みの総和: sum
	計算量: O(|E|log|V|)
*/

struct Kruskal {
	UnionFind uft;
	long long sum;  // 最小全域木の重みの総和
	vector<Edge> edges;
	int V;
	Kruskal(const vector<Edge> &edges_, int V_) : edges(edges_), V(V_) { init(); }
	void init() {
		sort(edges.begin(), edges.end(), comp_e); // 辺の重みでソート
		uft = UnionFind(V);
		sum = 0;
		for (auto e : edges) {
			if (!uft.isSame(e.u, e.v)) { // 閉路にならなければ加える
				uft.unite(e.u, e.v);
				sum += e.cost;
			}
		}
	}
};

int main() {

	int n;
	cin >> n;
	vector<vector<int>> VE(n, vector<int>(n));
	int counter=0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> VE[i][j];
			if (VE[i][j] != -1) { counter++; }
		}
	}

	int V, E;
	V = n;
	E = counter;
//	cin >> V >> E;
	vector<Edge> edges(E);
//	for (int i = 0; i < E; i++) {
	counter = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			long long s, t, w;
			//		cin >> s >> t >> w;
			if (VE[i][j] != -1) {
				s = i;
				t = j;
				w = VE[i][j];

				Edge e = { s, t, w };
				edges[counter] = e;
				counter++;
			}
		}
	}
//	}


	Kruskal krs(edges, V);
	cout << krs.sum << endl;
	return 0;
}