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>
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(114514){
    minv=INF;
    u=-1;
    for(int i=0;i<n;++i){
      if(minv>d[i] && color[i]!=BLA){
	u=i;
	minv=d[i];
      }
    }
    if(u==-1) break;
    color[u]=BLA;
    for(int v=0;v<n;++v){
      if(color[v]!=BLA && m[u][v]!=INF){
	if(d[v]>m[u][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;
      m[i][j]=(e==-1)?INF:e;
    }
  }
  cout<<prim()<<endl;
  return 0;
}

C++: #include <iostream>
 using namespace std;
 static const int WHITE = 0;
 static const int GRAY = 1;
 static const int BLACK =2;
 static const int MAX = 100;
 static const int INFTY = (1<<21);
 
 int n,M[MAX][MAX];
 
 int prim(){
 	int u,miv;
 	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){
 		u=-1;
 		miv = INFTY;
 		for(int j=0;j<n;j++){
 			if(color[j]!=BLACK&&d[j]<miv){
 				miv = d[j];
 				u=j;
 			}
 		}
 		if(u==-1) break;
 		
 		color[u] = BLACK;
 		
 		for(int i=0;i<n;i++){
 			if(color[i]!=BLACK&&M[u][i]!=INFTY){
 				if(d[i]>M[u][i]){
 					d[i] = M[u][i];
 					p[i] = u;
 					color[i] = 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;
 }

C++: #include <iostream>
#include <set>
using namespace std;
    int d[101];
    int G[101][101];
void krusta(int n){
    int color[101],l,m;
    for(int i=1;i<=n;i++){
        d[i]=1000000;
        color[i]=0;
    }
    d[1]=0;
    while(1){
        m=1000000;
        for(int i=1;i<=n;i++){
            if(color[i]!=1 && d[i]<m){
                m=d[i];
                l=i;
            }
        }
        color[l]=1;
        if(m==1000000){
            break;
        }
        for(int i=1;i<=n;i++){
            if(color[i]!=1 && G[l][i]!=-1 && G[l][i]<d[i]){
                d[i]=G[l][i];
            }
        }
    }
}
int main(){
    int n,m;
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>m;
            G[i][j]=m;
        }
    }
    krusta(n);
    int sum=0;
    for(int i=1;i<=n;i++){
        sum=sum+d[i];
    }
    cout<<sum<<endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;


struct edge{ int u, v, cost; };

bool comp(const edge& e1, const edge& e2){

  return e1.cost < e2.cost;

}

#define MAX_N 100000
#define MAX_E 100000

int par[MAX_N];

int rank[MAX_N];

void init(int n){

  for(int i = 0 ; i < n ; i++){

    par[i] = i;

    rank[i] = 0;

  }

}

int find(int x){

  if(par[x] == x) return x;

  else return par[x] = find(par[x]);

}

void unite(int x, int y){

  x = find(x);

  y = find(y);

  if(x == y) return;

  if(rank[x] == rank[y]) par[x] = y;

  else par[y] = x;

  if(rank[x] == rank[y]) rank[x]++;
 
}

bool same(int x, int y){

  return find(x) == find(y);

}

edge es[MAX_E];

int V, E;

int kruskal(){

  sort(es, es+E, comp);

  init(V);

  int res = 0;

  for(int i = 0 ; i < E ; i++){

    edge e = es[i];

    if(!same(e.u, e.v)){

      unite(e.u, e.v);

      res += e.cost;

    }

  }

  return res;

}

int main(){
  int n;
  cin>>n;
  V=n;
  int i,j,k;
  E=0;
  rep(i,n)rep(j,n){
    int t;
    cin>>t;
    if(t<0)continue;
    es[E].u=i;
    es[E].v=j;
    es[E].cost=t;
    E++;
  }
  cout<<kruskal()<<endl;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
int p[101];
void makeset(int N){
    for(int i=0;i<N;++i){
        p[i]=i;
    }
}
int root(int x){
    if(p[x]==x) return x;
    return (p[x]==root(p[x]));
}
void unite(int x,int y){
    p[root(x)]=root(y);
}
bool same(int x,int y){
    return root(x)==root(y);
}
int main(){
    int N,M=0;
    int A[101][101],cost[101],src[101],dst[101];
    cin>>N;
    makeset(N);
    for(int r=0;r<N;++r){
        for(int c=0;c<N;++c){
            cin>>A[r][c];
        }
        for(int c=r+1;c<N;++c){
            if(A[r][c]>-1){
                cost[M]=A[r][c];
                src[M]=r;
                dst[M]=c;
                M+=1;
            }
        }
    }

    
    pair<int,int>edges[101];
    for(int i=0;i<M;++i){
        edges[i]=make_pair(src[i],dst[i]);
    }
    sort(edges,edges+M);
    sort(cost,cost+M);
    int total=0,num=0,flag=0;
    for(int i=0;i<M;++i){
        unite(edges[i].first,edges[i].second);
        total+=cost[i];
        num++;
        if(num==N-1){
            flag=1;
            break;
        }
    }
    if(flag){
        cout<<total<<endl;
    }
}