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>
#include <algorithm>
#include <vector>
#include <map>
#include <cstring>
using namespace std;

typedef long long ll;

#define MAX_V 100
#define INF int(1e9)

// g : ??°??????, V : g???????????°, s : ????????????????????????
int Prim (int g[MAX_V + 1][MAX_V + 1], int V, int s) {
    ll ret = 0, min_, nxtv, v = s, size_;
    // bool used[V] = {false}; // c++11??????
    bool used[V]; memset(used, 0, sizeof(used)); // c++11??\???
    vector<int> usedv;
    usedv.push_back(s);
    used[s] = true;

    while (usedv.size() < V) {
        min_ = INF;
        size_ = usedv.size();

        // ?????¨???????????????????????°?????????????????\?¶????????????????????°????????????¢???
        for (int i = 0; i < size_; ++i) {
            for (int j = 0; j < V; ++j) {
                if (i == j) continue;
                if (!used[j] && min_ > g[usedv[i]][j]) {
                    min_ = g[usedv[i]][j];
                    v = j;
                }
            }
        }

        ret += min_;
        usedv.push_back(v);
        used[v] = true;
    }

    return ret;
}

int n, g[MAX_V + 1][MAX_V + 1];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> g[i][j];
            if (g[i][j] == -1) g[i][j] = INF;
        }
    }

    cout << Prim(g, n, 0) << endl;


    return 0;
}

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

const int MAX = 105;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
const int INFTY = 1 << 21;
int n;
int m[MAX][MAX];
int color[MAX];
int d[MAX];
int p[MAX];

int prim() {
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    p[i] = -1;
  }
  d[0] = 0;
  p[0] = -1;

  while (true) {
    int u {-1};
    int min_cost = INFTY;
    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < min_cost) {
        min_cost = d[i];
        u = i;
      }
    }
    if (u == -1) break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && m[u][v] < d[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;
}

C++: #include<iostream>
#include<algorithm>
#define MAXV 101
#define INF 2001
using namespace std;

int n, total = 0, mine, minj;
int mst[MAXV] = {}; //1 origin
int edge[MAXV][MAXV] = {}; //1 origin

void prim(void);
bool doesFinished(void);

int main() {
	int i, j;
	
	cin >> n;
	for (i = 1; i <= n; i++) {
		
		for (j = 1; j <= n; j++) {
			cin >> edge[i][j];
		}
	}

	mst[1] = 1;
	while (!doesFinished()) {
		prim();
	}

	cout << total << endl;

	return 0;
}

void prim(void) {
	int i, j;
	mine = INF;
	for (i = 1; i <= n; i++) {
		if (mst[i] == 0)continue;
		for (j = 1; j <= n; j++) {
			if (edge[i][j] == -1 || mst[j] == 1)continue;
			if (edge[i][j] < mine) {
				mine = edge[i][j];
				minj = j;
			}
		}
	}
	mst[minj] = 1;
	if(mine != INF)total += mine;
}

bool doesFinished(void) {
	for (int i = 1; i <= n; i++) {
		if (mst[i] == 0)return false;
	}
	return true;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include <cstdlib>  
#include <cmath>   
#include<cctype>
#include<string>
#include<set>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include <deque>
#include <typeinfo>
#include <utility> 

#define rep(i,m,n) for(int i = m;i < n;++i)
using namespace std;
using ll = long long;
using R = double;
const ll inf = 1LL << 50;
const ll MOD = 1e9 + 7;

int infty = (1 << 21);
int cost[100][100];
int mincost[100];
bool used[100];
int V;

int prim() {
	for (int i = 0; i < V; ++i) {
		mincost[i] = infty;
		used[i] = false;
	}
	mincost[0] = 0;
	int res = 0;

	while (1) {
		int v = -1;
		for (int u = 0; u < V; u++) {
			if (!used[u] && (v == -1 || mincost[u] < mincost[v])) {
				v = u;
			}
		}
		if (v == -1)break;
		used[v] = true;
		res += mincost[v];

		for (int u = 0; u < V; u++) {
			mincost[u] = min(mincost[u], cost[v][u]);
		}
	}
	return res;
}


int main() {
	cin >> V;
	rep(i, 0, V)rep(j, 0, V) {
		int e; cin >> e;
		cost[i][j] = (e == -1) ? infty : e;
	}

	cout << prim() << endl;
}

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

struct Edge {
    int from, to, cost;
    Edge(int f, int t, int c) : from(f), to(t), cost(c) { }
    bool operator <(const Edge& e) const {
        return cost < e.cost;
    }
};

struct UnionFind {
    vector<int> data;
    UnionFind(int n) : data(n, -1) { }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }

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

    void merge(int x, int y) {
        x = root(x), y = root(y);
        if(x != y) {
            data[x] += data[y]; data[y] = x;
        }
    }
};

int main() {
    int N; cin >> N;
    vector<Edge> es;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            int c; cin >> c;
            if(c != -1) {
                es.push_back(Edge(i, j, c));
            }
        }
    }
    sort(es.begin(), es.end());
    UnionFind uf(N);
    int ans = 0;
    for(int i = 0; i < (int)es.size(); i++) {
        int from = es[i].from;
        int to = es[i].to;
        int cost = es[i].cost;
        if(uf.same(from, to)) continue;
        uf.merge(from, to);
        ans += cost;
    }
    cout << ans << endl;
}