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;
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]; 
    // Tに属する頂点とV-Tに属する頂点をつなぐ辺の中で、重みが最小の辺の重みを記録
    // MSTにおける頂点vの親を記録
    // 訪問状態を記録

    for(int i=0; i<n; i++){ // 全ての頂点に関して、colorをwhite、dをinftyに初期化
        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;
        for(int v=0; v<n; v++){
            if(color[v] != BLACK && M[u][v] != INFTY){ // 未訪問かつuからvに辺があるとき
                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(int argc, char const *argv[])
{
    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 <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
#include <map>
#include <set>
#include <cstdlib>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

char ToUpper(char cX) { return toupper(cX); }
char Tolower(char cX) { return tolower(cX); }

const long long INF = 1LL << 60;
const long long MOD = 1000000007;

using namespace std;
typedef unsigned long long ull;
typedef  long long ll;



vector<vector<ll>> dp;

class UnionFind {
public:
	vector<ll> Parent;
	UnionFind(ll N) {
		Parent = vector<ll>(N, -1);
	}
	ll root(ll A) {
		if (Parent[A] < 0) {
			return A;
		}
		return Parent[A] = root(Parent[A]);
	}

	ll size(ll A) {
		return -Parent[root(A)];
	}

	bool connect(ll A, ll B) {
		A = root(A);
		B = root(B);
		if (A == B) {
			return false;
		}
		if (size(A) < size(B)) {
			swap(A, B);
		}

		Parent[A] += Parent[B];
		Parent[B] = A;

		return true;
	}

};

class Edge {
public:
	ll source, target, cost;
	Edge(ll source = 0, ll target = 0, ll cost = 0) :
		source(source), target(target), cost(cost) {}
	bool operator<(const Edge &e)const {
		return cost < e.cost;
	}
};

ll kuraskal(ll N, vector<Edge> &edges) {
	ll totalCost = 0;
	sort(edges.begin(), edges.end());
	UnionFind uni(N + 1);
	ll source, target;
	for (ll i = 0; i < edges.size(); i++) {
		Edge e = edges[i];
		if (uni.root(e.source) != uni.root(e.target)) {
			totalCost += e.cost;
			uni.connect(e.source, e.target);
		}
	}
	return totalCost;
}




int main()
{
	ll n;
	cin >> n;
	vector<vector<ll>> a;
	a.resize(n);
	for (int i = 0; i < n; i++) {
		a[i].resize(n);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a[i][j];
		}
	}
	vector<Edge> edges;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (a[i][j] != -1) {
				edges.push_back(Edge(i, j, a[i][j]));
			}
		}
	}

	cout << kuraskal(n, edges) << endl;
	return 0;
}

C++: //
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define INFTY 20000
#define NIL -1

enum Color {
    White,
    Glay,
    Black,
};

using namespace std;

int n;
int w[MAX_N][MAX_N];
int d[MAX_N], p[MAX_N];
Color color[MAX_N];

int min(int x, int y) {
    if(x < y) return x;
    return y;
}

int solve() {
    d[0] = 0;

    while(true) {
        int u = NIL, c = INFTY;

        for(int i=0; i<n; i++) {
            if(d[i] < c && color[i] != Black) {
                u = i;
                c = d[i];
            }
        }

        if(u == NIL)
            break;

        color[u] = Black;

        for(int i=0; i<n; i++) {
            if(color[i] != Black && w[u][i] != INFTY) {
                if(d[i] > w[u][i]) {
                    d[i] = w[u][i];
                    p[i] = u;
                    color[i] = Glay;
                }
            }
        }
    }


    int tc = 0;
    for(int i=0; i<n; i++) {
        if(p[i] != NIL)
            tc += w[i][p[i]];
    }
    return tc;
}

int main() {
    for (int i = 0; i < MAX_N; i++) {
        d[i] = INFTY;
        p[i] = NIL;
        color[i] = White;
        for (int j = 0; j < MAX_N; j++)
            w[i][j] = INFTY;
    }

    int p;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> p;
            w[i][j] = (p == -1) ? INFTY : p;
        }
    }

    cout << solve() << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main(){
  int n;
  cin >> n;
  vector<vector<int>> gr(n, vector<int>(n));
  rep(i, n){
    rep(j, n){
      int in;
      cin >> in;
      gr.at(i).at(j) = in;
    }
  }
  vector<bool> chk(n, false);
  int ans = 0;
  priority_queue<pair<int, int>> pq;
  pq.push(make_pair(0, 0));
  int co = 0;
  while(!pq.empty()){
    auto p = pq.top(); pq.pop();
    if(chk.at(p.second) == true) continue;
    chk.at(p.second) = true;
    ans += p.first;
    // cerr << ans << endl;
    co++;
    if(co == n) break;

    rep(i, n){
      if(gr.at(p.second).at(i) == -1) continue;
      if(chk.at(i) == true) continue;
      pq.push(make_pair(-1*gr.at(p.second).at(i), i));
    }
  }
  cout << -1*ans << endl;
  return 0;
}