Go: package main

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

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0

	for {
		minv = INFTY
		u = -1
		for 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 v := 0; v < n; v++ {
			if color[v] != BLACK && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

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

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

const int MAX = 100, INF = 1e9, white = 0, gray = 1, black = 2;
int G[MAX][MAX], color[MAX], d[MAX], p[MAX], n;

int prim(){
    int u, minv;
    for (int i = 0; i < n; i++)
    {
        d[i] = INF;
        p[i] = -1;
        color[i] = white;
    }
    d[0] = 0;
    while (true)
    {
        minv = INF;
        u = -1;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != black && d[i] < minv)
            {
                minv = d[i];
                u = i;
            }
        }
        if (u == -1)
        {
            break;
        }
        color[u] = black;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != black && G[u][i] != INF)
            {
                if (d[i] > G[u][i])
                {
                    d[i] = G[u][i];
                    p[i] = u;
                    color[i] = gray;
                }
                
            }
            
        }
        
    }
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        if (p[i] != -1){
            sum += G[i][p[i]];
        }
    }
    return sum;
}

int main(){
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int v;
            cin >> v;
            G[i][j] = (v == -1) ? INF : v;
        }
    }
    cout << prim() << endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int P[10000];
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()
{
  int N;
  cin >> N;
  int A[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int x;
      cin >> x;
      A[j][i] = x;
      // cout << "cost: " << x << " = " <<  j << "->"  << i << endl;
    }
  }
  // cout << "-----エッジ-------" << endl;
  int cost[10000], src[10000], dst[10000];
  int M = 0;
  for (int r = 0; r < N; r++)
  {
    for (int c = r + 1; c < N; c++)
    {
      if (A[r][c] > -1)
      {
        cost[M] = A[r][c];
        src[M] = r;
        dst[M] = c;
        // cout << "cost: " << cost[M] << " = " <<  src[M] << "->"  << dst[M] << endl;
        M += 1;
      }
    }
  }
  // cout << "M = " << M << " N = " << N << endl;
  init(N);
  pair<int, pair<int, int> > edges[M + 1];
  for (int m = 0; m < M; m++)
  {
    edges[m] = make_pair(cost[m], make_pair(src[m], dst[m]));
  }
  // cout << "-----sort前-------" << endl;
  // for (int i = 0; i < M; i++)
  //     cout << "cost: " << edges[i].first << " = " << edges[i].second.first << "->"  << edges[i].second.second << endl;
  sort(edges, edges+M);
  // cout << "-----sort後-------" << endl;
  // for (int i = 0; i < M; i++)
  //     cout << "cost: " << edges[i].first << " = " << edges[i].second.first << "->"  << edges[i].second.second << endl;
  int sum = 0;
  for (int i=0; i<M; i++) {
    if (is_same_set(edges[i].second.first, edges[i].second.second))
      continue;
    unite(edges[i].second.first, edges[i].second.second);
    sum += edges[i].first;
  }
  cout << sum << endl;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
// Graph II - Minimum Spanning Tree
// TODO: REVIEW

#define SUBMIT

#include <utility>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <limits>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <cstring>
#include <climits>
#include <memory>

using namespace std;
using ui64 = unsigned long long;
using i64 = long long;

enum class color_type {
    white,
    gray,
    black
};

const int INF = INT_MAX;
const ui64 MAX_N = 100;
int adj[MAX_N][MAX_N];
int n;

int prim() {
    int d[MAX_N], p[MAX_N];
    color_type color[MAX_N];

    for (int i = 0; i < n; ++i) {
        d[i] = INF;
        p[i] = -1;
        color[i] = color_type::white;
    }

    d[0] = 0;

    while (true) {
        // 未訪問のノード
        int u = -1, min_v = INF;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min_v && color[i] != color_type::black) {
                u = i;
                min_v = d[i];
            }
        }

        if (u == -1) break;

        color[u] = color_type::black;
         for (int v = 0; v < n; ++v) {
            if (color[v] != color_type::black && adj[u][v] != INF) {
                if (adj[u][v] < d[v]) {
                    d[v] = adj[u][v];
                    p[v] = u;
                    color[v] = color_type::gray;
                }
            }
        }
    }

    int sum = 0;
    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) sum += adj[i][p[i]];
    }

    return sum;
}

int main() {
#ifdef SUBMIT
    auto& stream = cin;
#else
    string program_input_str = R"(20
 -1 100 2 -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 100 -1 3 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 2 3 -1 1 10 12 -1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)";
    stringstream stream(program_input_str);
#endif
    memset(adj, 0, sizeof(adj));
    stream >> n;

    int w;
    for (int y = 0; y < n; ++y) {
        for (int x = 0; x < n; ++x) {
            stream >> w;
            if (w == -1) adj[y][x] = INF;
            else adj[y][x] = w;
        }
    }

    cout << prim() << endl;
    return 0;
}