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 <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll>P;
#define INF 10e16
#define MOD 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define fi first
#define se second
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define PI acos(-1.0)
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int cmp(P a,P b){
        if(a.fi!=b.fi)
        return a.fi>b.fi;
        else
        return a.se<b.se;
}
//---------------------------------------------------------------------------
#define INFTY 2001
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 100

// 最小全域木を構成し、重みの総和を返す関数
int prim(int n, int A[N][N]) {
  int i, vertex, min_cost, sum_weight, cost[N], color[N];
  // 初期化
  for (i = 0; i < n; i++) {
    cost[i] = INFTY;
    color[i] = WHITE;
  }
  cost[0] = 0;
  color[0] = GRAY;
  sum_weight = 0;
  
  while (1) {
    // Tに隣接する頂点の中で最小のコストを持つものを選ぶ
    vertex = -1;
    min_cost = INFTY;
    for (i = 0; i < n; i++) {
      if (color[i] == GRAY && cost[i] < min_cost) {
        min_cost = cost[i];
        vertex = i;
      }
    }
    // T = V ならば終了
    if (vertex == -1) break;

    color[vertex] = BLACK;  // vertexをTに加える
    sum_weight += cost[vertex];

    // vertexに隣接し、Tに属していない頂点のコストを更新
    for (i = 0; i < n; i++) {
      if (color[i] != BLACK && A[vertex][i] < INFTY) {
        color[i] = GRAY;
        if (cost[i] > A[vertex][i]) cost[i] = A[vertex][i];
      }
    }
  }

  return sum_weight;
}

/*
// 最小全域木の辺の重みの和を計算する関数
int sum_weight(int n, int parent[], int A[N][N]) {
  int sum = 0;
  
  for (int i = 1; i < n; i++) sum += A[i][parent[i]];
  
  return sum;
}*/

//---------------------------------------------------------------------------
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    int i, j, n, w, A[N][N];
    cin >> n;
    // 隣接行列が与えられる
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        cin >> w;
        if (w == -1) A[i][j] = INFTY;
        else A[i][j] = w;
      }
    }

    cout << prim(n, A) << endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool f[100];
int n;
int tr[100];


int trmin(){
  int res=10000,resi;
  for(int i=0;i<n;i++)
    if(f[i]==0 && tr[i]<res) res = tr[i],resi=i;
  return resi;
}


int main (){

  int mp[100][100];

  cin >> n;
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      cin >> mp[i][j];

  for(int i=0;i<n;i++) f[i]=0;
  for(int i=0;i<n;i++) tr[i]=10000;
  tr[0] = 0;
  int trsum=0;

  for(int k=0;k<n;k++){
    int r=trmin();
    f[r]=1;
    trsum+=tr[r];

    for(int i=0;i<n;i++) 
      if(mp[r][i]!=-1)
	tr[i] = min(tr[i],mp[r][i]);
  }
  cout<<trsum<<endl;

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF 2*1e9
int main(){
  
  int n;
  int cost[100][100];
  int mincost[100];
  int used[100]={};
  
  cin>>n;
  for(int i=0;i<n;i++){
    mincost[i]=INF;
    for(int j=0;j<n;j++){
      cin>>cost[i][j];
      if(cost[i][j]==-1)cost[i][j]=INF;
    }
  }

  mincost[0]=0;
  int ans=0;

  while(1){
    int v=-1;
    for(int u=0;u<n;u++){
      if(used[u]==0&&(v==-1||mincost[u]<mincost[v]))v=u;
    }
    
    if(v==-1)break;
    used[v]=1;
    ans+=mincost[v];
    
    for(int u=0;u<n;u++){
      mincost[u]=min(mincost[u],cost[v][u]);
    }
  }
  
  cout<<ans<<endl;
  
  return 0;
  
}

C++: #include<iostream>
using namespace std;
#define MAX 1000
#define Infty 10000

struct Edge{int cost, left, right; };

void Bubble(struct Edge A[], int N){
  for(int i=0;i<N;i++){
    for(int j=N-1;j>=i;j--){
      if(A[j].cost < A[j-1].cost){
        Edge buffa = A[j];
        A[j] = A[j-1];
        A[j-1] = buffa;
      }
    }
  }
}
void print(struct Edge A[], int N){
  for(int i=0;i<N;i++){
    cout << A[i].cost << A[i].left << A[i].right << endl;
  }
}

int find(int x, int parents[]){
  int c = x;
  while(parents[c]!=c) c = parents[c];
  return c;
}

void uunion(int x, int y, int parents[]){
  int a = find(x,parents);
  int b = find(y,parents);
  if(a!=b) parents[a] = b;
}

int main(){
    int n, M[MAX][MAX], d=0;
    int k = 0;
    struct Edge A[MAX];
    cin >> n;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        cin >> M[i][j];
        if(M[i][j]==-1) M[i][j] = Infty;
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        if(j<i){
          //cout << "k = " << k << endl;
          A[k].cost = M[i][j];
          //cout << "M[1][1]=" << M[i][j] << endl;
          //cout << "A.cost = " << A[k].cost << endl;
          A[k].right = i;
          A[k].left = j;
          k++;
        }
      }
    }
    Bubble(A, k);
    
    int parents[k];
    for(int i=0;i<k;i++) parents[i] = i;
    for(int i=0;i<k;i++){
      if(find(A[i].left,parents)!=find(A[i].right,parents)){
        d += A[i].cost;
        uunion(A[i].left,A[i].right,parents);
      }
    }
    cout << d << endl;

    return 0;
}