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 <algorithm>
#include <iostream>
using namespace std;

int P[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;
  else {
    P[a] = root(P[a]);
    return 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(){
  int N;
  cin >> N;
  pair <int, int> edge[10010];
  for (int i=0; i<N; i++){
    for (int j=0; j<N; j++){
      cin >> edge[i*N+j].first;
      edge[i*N+j].second = i*N+j;
    }
  }
  sort(edge, edge+N*N);
  init(N);
  int ans = 0;
  int b = 0;
  for (int k=0; k<N*N; k++){
    while(edge[k].first < 0) k++;
    int i = edge[k].second / N;
    int j = edge[k].second % N;
    if (is_same_set(i,j)){}
    else{
      unite(i,j);
      ans += edge[k].first;
    }
  }
  cout << ans << endl;
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

int N;
const int MAX_E=100;
int label[MAX_E];
priority_queue<int,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > > pq;

int main(){
  cin>>N;
  REP(i,N)
    label[i]=i;
  REP(i,N){
    REP(j,N){
      int a;cin>>a;
      if(a>=0&&i>j){
	pq.push(make_pair(a,make_pair(j,i)));
      }
    }
  }
  int size=0;
  while(!pq.empty()){
    int cost=pq.top().first;
    pair<int,int> e=pq.top().second;
    pq.pop();
    if(label[e.first]!=label[e.second]){
      size+=cost;
      REP(i,N){
	if(label[i]==label[e.second])
	  label[i]=label[e.first];
      }
    }
  }
  printf("%d\n",size);
}

C++: #include "stdafx.h"
#include <iostream>
using namespace std;

int main(void)
{
	int n, now = 0, flag[101] = {};
	long ans = 0, an[100][100], min[100];

	cin >> n;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++)
			cin >> an[i][j];
		min[i] = 1000000000;
	}

	do {
		long key = LONG_MAX, key_i;
		flag[now] = 1;
		flag[100] = 0;

		for (int i = 0; i < n; i++) {
			if (an[now][i] != -1 && an[now][i] < min[i] && flag[i] == 0)
				min[i] = an[now][i];
			if (flag[i] == 0 && min[i] < key) {
				key = min[i];
				key_i = i;
			}
			if (flag[i] == 1)
				flag[100]++;
		}

		now = key_i;
		ans += key;

	} while (flag[100] != n - 1);

	cout << ans << endl;

	return 0;
}

C++: #include <cstdio>
#include <cstring>
#define MAXN (1 << 21)
int g[101][101], d[101], n;
bool vis[101];

int mst()
{
  int sum = 0;
  d[0] = 0;
  for (int j = 0; j < n; j++)
  {
    int u = -1;
    int mini = MAXN;
    for (int i = 0; i < n; i++) // take node from V-T to T
    {
      if (d[i] < mini && !vis[i])
      {
        u = i;
        mini = d[i];
      }
    }
    if (u == -1) return -1;
    sum += mini;
    vis[u] = true;
    for (int v = 0; v < n; v++) // update smallest weight
    {
      if (!vis[v] && g[u][v] >= 0 && d[v] > g[u][v])
      {
        d[v] = g[u][v];
      }
    }
  }
  return sum;
}

int main()
{
  memset(vis, false, sizeof(vis));
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
  {
    d[i] = MAXN;
  }
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)
    {
      scanf("%d", &g[i][j]);
    }
  }
  printf("%d\n", mst());
}

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

#define N 101
#define INF 2010
int n;

int A[N][N];
int color[N];
int d[N];

int mst(int root) {
    int totalW = 0;
    d[root] = 0;

    while(true) {
        int added = -1;
        int minw = INF;
        for (int i = 0; i < n; i++) {
            if (color[i] == 0 && d[i] < minw) {
                added = i;
                minw = d[i];
            }
        }

        if (added == -1) {
            break;
        }

        totalW += minw;
        color[added] = 1;
        for (int i = 0; i < n; i++) {
            if (color[i] == 0 && A[added][i] < d[i]) {
                d[i] = A[added][i];
            }
        }
    }

    return totalW;
}

int main() {
    cin >> n;

    int w;
    for (int i = 0; i < n; i++) {
        color[i] = 0;
        d[i] = INF;
        for (int j = 0; j < n; j++) {
            cin >> w;
            if (w == -1) A[i][j] = INF;
            else A[i][j] = w;
        }
    }

    cout << mst(0) << endl;

    return 0;
}

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

int x[10000][10000];
int MIN[10000];
int MINX = 0;
int MINZ = 10000000;

int main() {
	memset(MIN, 127, sizeof(MIN));
	int n, a;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a;
			x[i][j] = a;
		}
	}
	for(int i=0;i<n;i++){
	    for(int j=0;j<n;j++){
	        if(x[i][j]!=-1){
	            MIN[i]=min(x[i][j],MIN[i]);
	        }
	    }
	}
	for(int i=0;i<n;i++){
	    MINX=0;
	    for(int j=0;j<n;j++){
	        MINX += MIN[j];
	    }
	    MINX -= MIN[i];
	    MINZ=min(MINZ,MINX);
	}
	cout<<MINZ<<endl;
	return 0;
}

C++: #define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#define INF 20000202
#define maxn 101
using namespace std;

int M[maxn][maxn] = {}, d[maxn] = {}, p[maxn] = {}, s[maxn] = {};
int n;

void Prim()
{
	for (int i = 0; i < n; i++)
		d[i] = INF;
	d[0] = 0;
	p[0] = -1;
	while (true)
	{
		int u = -1, mincost = INF;
		for(int i=0;i<n;i++)
			if (s[i] == 0 && d[i] < mincost)
			{
				mincost = d[i];
				s[i] = 1;
				u = i;
			}
		if (u == -1)break;
		for(int v=0;v<n;v++)
			if (M[u][v] != -1 && M[u][v] < d[v] && s[v] == 0)
			{
				d[v] = M[u][v];
				p[v] = u;
			}
	}
}

int main()
{
	int sum = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			scanf("%d", &M[i][j]);
	Prim();
	for (int i = 0; i < n; i++)
	{
		sum += d[i];
	}
	printf("%d", sum);
	return 0;
}