Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

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

C++: #include <cstdio>
#include <climits>
#include <vector>

int w[100][100];
std::vector<int> mst;
std::vector<int> others;
typedef std::vector<int>::iterator itr;

int minimum(){
    if(others.size()<=0)return 0;
    int candiW=INT_MAX;
    itr candiI;
    for(itr i=mst.begin();i!=mst.end();i++){
        for(itr j=others.begin();j!=others.end();j++){
            if(w[*i][*j]<candiW){
                candiW = w[*i][*j];
                candiI = j;
            }
        }
    }
    mst.push_back(*candiI);
    others.erase(candiI);
    return candiW + minimum();
}

int main(){
    int i, j, n;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            scanf("%d",&w[i][j]);
            if(w[i][j]==-1)w[i][j]=INT_MAX;
        }
    }
    mst.push_back(0);
    for(i=1;i<n;i++)others.push_back(i);
    printf("%d\n",minimum());
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Edge {
    ll s,t;
    ll w;
    bool operator < (const Edge& tgt) const {
	return w > tgt.w;
    }
};

ll Prim(const vector<vector<Edge>>& G) {
    const ll V = G.size();
    priority_queue<Edge> edges;
    
    vector<int> used(V,0);
    edges.push( Edge{-1,0,0} );
    ll ans = 0;
    while( edges.size() ) {
	Edge e = edges.top(); edges.pop();

	if( used[e.t] == 1 ) continue;
	used[e.t] = 1;
	ans += e.w;

	for(auto& nxt : G[e.t]) {
	    if( used[nxt.t] == 1 ) continue;
	    edges.push(nxt);
	}
    }
    return ans;
}


int main () {

    ll V; cin >> V;
    vector<vector<Edge>> G(V);
    for(int i=0; i<V; i++) {
	for(int j=0; j<V; j++) {
	    ll w; cin >> w;
	    if( w != -1 ) {
		G[i].push_back( Edge{i,j,w} );
	    }
	}
    }
    cout << Prim(G) << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using pii = pair<int, int>;

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

//pow(llpow,modpow)
template <class T>
ll llpow(ll x, T n)
{
    ll ans = 1;
    if (x == 0)
        ans = 0;
    while (n)
    {
        if (n & 1)
            ans *= x;
        x *= x;
        n >>= 1;
    }
    return ans;
}
long long modpow(long long a, long long n, long long mod)
{
    long long res = 1;
    while (n > 0)
    {
        if (n & 1)
            res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
//最大公約数
template <class T>
inline T gcd(T x, T y)
{
    if (y == 0)
        return x;
    else
    {
        return gcd(y, x % y);
    }
}
//最小公倍数
template <class T>
inline T lcm(T x, T y) { return x / gcd(x, y) * y; }
//逆元
long long modinv(long long a, long long m)
{
    long long b = m, u = 1, v = 0;
    while (b)
    {
        long long t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0)
        u += m;
    return u;
}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)

long long INF = 1LL << 60;
int main()
{
    int n;
    cin >> n;
    vector<vi> a(n,vector<int> (n));
    rep(i, 0, n)
    {
        rep(j, 0, n)
        {
            cin >> a[i][j];
            if(a[i][j]==-1)a[i][j]=1<<30;
        }
    }
    ll res = 0;
    set<int> rem,visited;
    visited.insert(0);
    rep(i,1,n)rem.insert(i);
    while(!rem.empty()){
        int minv=1<<30,mini=-1;
        for(int prev:visited){
            for(int next:rem){
                if(a[prev][next]<minv){
                    minv=a[prev][next];
                    mini=next;
                }
            }
        }
        res+=minv;
        rem.erase(mini);
        visited.insert(mini);
    }
    cout<<res<<endl;
    return 0;
}