Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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

int n,m=0,tmp,p[110],ans=0,a,b,c; //m:辺の数
tuple<int,int,int> line[10010]; //辺

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(){
    cin>>n;
    init(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>tmp;
            if(j>i){
                if(tmp!=-1){
                    line[m]=make_tuple(tmp,i,j);
                    m++;
                }
            }
        }
    }
    sort(line,line+m);
    init(n);
    for(int i=0;i<m;i++){
        c=get<0>(line[i]);
        a=get<1>(line[i]);
        b=get<2>(line[i]);
        if(!(is_same_set(a,b))){
            unite(a,b);
            ans+=c;
        }
        //cout<<c<<a<<b<<endl;
    }
    cout<<ans<<endl;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
using namespace std;

struct Edge{
  int src,dst;
  int weight;
  Edge(int src,int dst,int weight):
    src(src),dst(dst),weight(weight){}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

bool operator < (const Edge &m,const Edge &n){
  return m.weight!=n.weight ? m.weight>n.weight :
    m.src!=n.src ? m.src>n.src : m.dst>n.dst;
}

struct UnionFind{
  vector<int> data;
  UnionFind(int size):
    data(size,-1){}

  int root(int x){
    return data[x]<0 ? x :
      data[x]=root(data[x]);
  }

  void unionset(int x,int y){ //bool->void
    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 findset(int x,int y){
    return root(x)==root(y);
  }
};

/*test用関数
void PrintGraph(Graph);
void PrintEdges(Edges);
void PrintPriority_queue(priority_queue<Edge>);
*/

pair<int,Edges> MinimumSpinningForest(Graph g){
  int n=g.size();
  UnionFind uf(n);
  priority_queue<Edge> que;

  for(int i=0;i<n;i++){
    for(Edges::iterator l=g[i].begin();l!=g[i].end();l++){
      if((*l).src<(*l).dst && (*l).weight>0)
	que.push(*l);
    }
  }

  //PrintPriority_queue(que);
  int sum=0;
  Edges select;

  while((int)select.size()<n && !que.empty()){
    Edge tmp=que.top();que.pop();

    if(!uf.findset(tmp.src,tmp.dst)){
      uf.unionset(tmp.src,tmp.dst);
      select.push_back(tmp);
      sum+=tmp.weight;
    }
  }

  return pair<int,Edges>(sum,select);
}


int main(int argc,char* argv[]){
  Graph g;
  int n,x;
  pair<int,Edges> p;

  cin>>n; 
  for(int i=1;i<=n;i++){
    for(int l=1;l<=n;l++){
      cin>>x;
      Edge tmp(i,l,x);
      Edges input;input.push_back(tmp);
      g.push_back(input);
    }
  }

  //PrintGraph(g);

  p=MinimumSpinningForest(g);

  //PrintEdges(p.second);
  
  cout<<p.first<<endl;
  return 0;
}

C++: # include<iostream>
using namespace std;
const int MAX = 100;
const int INFTY = (1 << 21);
const int WHITE = 0;
const int GREY = 1;
const int BLACK = 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] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
	}

	d[0] = 0;

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

		if (u == -1) break;
		color[u] = BLACK;

		for (int 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] = GREY;
				}
			}
		}
	}

	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) 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;
			M[i][j] = (e == -1) ? INFTY : e;
		}
	}

	cout << prim() << endl;

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#define P pair<int , int>
using namespace std;

int main(){
  int n, a[109][109];
  bool check[109];
  priority_queue<P, vector<P>, greater<P> > Q;
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++)cin >> a[i][j];
  }
  memset(check, false, sizeof(check));
  int ans = 0;
  Q.push(P(0, 0));
  while(!Q.empty()){
    P p = Q.top();Q.pop();
    if(check[p.second])continue;
    ans += p.first;
    check[p.second] = true;
    for(int i=0;i<n;i++){
      if(!check[i] && a[p.second][i]!=-1){
        Q.push(P(a[p.second][i], i));
      }
    }
  }
  cout << ans << endl;
}

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;


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] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int 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 (int 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;
        }
      }
    }
  }
  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;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}