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>
#include<vector>
#include<queue>
#include<utility>
#include<algorithm>
#define fi first
#define se second

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair< int,pair<int,int> > pp;

vi P;
vector<pp> d;

inline void init()
{
  int n;
  vvi a;
  cin >> n;
  P.resize(n);
  for(int i = 0; i < n; ++i)
    P[i] = i;
  a.resize(n);
  for(int i = 0; i < n; ++i){
    a[i].resize(n);
    for(int j = 0; j < n; ++j)
      cin >> a[i][j];
  }
  for(int i = 0; i < n; ++i)
    for(int j = i + 1; j < n; ++j)
      if(a[i][j] != -1){
	d.push_back(make_pair(a[i][j],make_pair(i,j)));
      }
  sort(d.begin(),d.end());
}

inline int root(int a)
{
  if(P[a] == a) return a;
  else return (P[a] = root(P[a]));
}

inline bool same(int a, int b)
{
  return root(a) != root(b);
}

inline void unite(int a, int b)
{
  P[root(a)] = root(b);
}

inline int answer()
{
  int answer = 0;
  for(size_t i = 0; i < d.size(); ++i){
    if(same(d[i].se.fi,d[i].se.se)){
      unite(d[i].se.fi,d[i].se.se);
	answer += d[i].fi;
    }
  }
  return answer;
}

int main()
{
  init();
  cout << answer() << endl;
  return 0;
}

C++: #include<iostream>

using namespace std;

static const int MAX = 100;
static const int NIL = (1 << 21);
static const int WHITE = 0;
static const int GREY = 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] = INFY;
    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; i < n; i++) {
      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;
}

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

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

int n;
vector<edge> G;

bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

class UnionFind {
 public:
    int g_num;
    vector<int> rank, par;

    UnionFind() {}
    UnionFind(int size) {
        rank.resize(size, 0);
        par.resize(size, 0);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
        g_num = size;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        g_num--;
        if (rank[x] > rank[y]) {
            par[y] = x;
        } else {
            par[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }

    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    int group_num() {
        return g_num;
    }
};

int kruscal() {
    sort(G.begin(), G.end(), comp);
    UnionFind uf(n);
    int res = 0;
    for (auto e: G) {
        if (!uf.same(e.u, e.v)) {
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int cost;
            cin >> cost;
            if (cost == -1) continue;
            G.push_back(edge {i, j, cost});
        }
    }
    int ans = prim();
    cout << ans << endl;
    return 0;
}

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

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

bool comp(const edge &e1, const edge &e2)
{
    return e1.cost < e2.cost;
}

int n, E, A[100][100];
int p[10010];
vector<edge> es(0);

void init(int n)
{
    rep(i, n) 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 kruskal()
{
    int sum = 0;
    E = 0;
    rep(i, n) rep(j, i)
    {
        if (A[i][j] != -1)
        {
            E++;
            edge e;
            e.u = i;
            e.v = j;
            e.cost = A[i][j];
            es.push_back(e);
        }
    }
    sort(es.begin(), es.end(), comp);
    rep(i, E)
    {
        edge e = es[i];
        if (!is_same_set(e.u, e.v))
        {
            unite(e.u, e.v);
            sum += e.cost;
        }
    }
    return sum;
}

int main()
{
    cin >> n;
    rep(i, n) rep(j, n) cin >> A[i][j];
    init(n);
    int ans = kruskal();
    cout << ans << endl;
    return 0;
}

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;
}