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 N = 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[N][N];

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

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

	d[0] = 0;

	while (1)
	{
		int u = -1;
		int minv = INFTY;
		for (int i = 0; i < n; ++i)
		{
			if (minv > d[i] && color[i] != BLACK)
			{
				minv = d[i];
				u = i;
			}
		}

		if (u == -1) break;
		color[u] = BLACK;

		for (int v = 0; v < n; ++v)
		{
			if (m[u][v] != INFTY && color[v] != BLACK && m[u][v] < d[v])
			{
				d[v] = m[u][v];
				p[v] = u;
				color[v] = GRAY;
			}
		}
	}

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

	return s;
}

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 <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

// ---------------------
// repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int main() {
  int n;
  cin >> n;

  int G[n][n];
  int visited[n];
  int d[n];
  int p[n];
  REP(y, n) {
    REP(x, n) {
      int cost;
      cin >> cost;

      if (cost == -1) {
        G[y][x] = 1 << 15;
      } else {
        G[y][x] = cost;
      }
    }
    visited[y] = 0;
    d[y] = 1 << 15;
    p[y] = -1;
  }

  d[0] = 0;

  REP(_, n) {
    int mincost = 1 << 15;
    int minv = -1;
    REP(v, n) {
      if (!visited[v] && d[v] < mincost) {
        minv = v;
        mincost = d[v];
      }
    }
    visited[minv] = 1;
    REP(to, n) {
      if (!visited[to] && G[minv][to] < (1 << 15) && G[minv][to] < d[to]) {
        d[to] = G[minv][to];
        p[to] = minv;
      }
    }
  }

  ll sum = 0;
  REP(i, n) {
    if (p[i] != -1) {
      sum += G[i][p[i]];
    }
  }

  cout << sum << endl;
  return 0;
}

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

struct Edge{
  int weight;
  int from;
  int to;
  Edge(int w,int f,int t):weight(w),from(f),to(t){}
};

const int inf = 1<<28;

bool operator<(Edge e1,Edge e2){
  return (e1.weight < e2.weight);
}

int nlist[110];

int parent(int i){
  if(nlist[i] == i) return i;
  else return (nlist[i] = parent(nlist[i]));
}

int main(){
  int n;
  cin>>n;
  vector<Edge> Elist;
  for(int i=0;i<n;i++) nlist[i]=i;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int temp;
      cin>>temp;
      if(temp != -1){
	Elist.push_back(Edge(temp,i,j));
      }
    }
  }
  sort(Elist.begin(),Elist.end());
  int ans = 0;
  for(int i=0;i<Elist.size();i++){
    Edge e = Elist[i];
    int fp = parent(e.from);
    int tp = parent(e.to);
    if(fp != tp){
      ans += e.weight;
      nlist[fp] = tp;
    }
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for(LL i = 0; i < n; ++i)
#define FOR(i, a, n) for(LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

//#define int long long
// 0-indexedに注意
template<typename T>
struct Edge {
    int to;
    T cost;
    Edge(int to, T cost) : to(to), cost(cost) {}
};

template<typename T>
class Prim{
    private:
        vector<vector<Edge<T>>> graph;
        vector<bool> used;
    public:
        Prim(int n);
        void AddEdge(int from, int to, T cost);
        T Run();
};

template<typename T>
Prim<T>::Prim(int n): graph(n), used(n, false) {}

template<typename T>
void Prim<T>::AddEdge(int from, int to, T cost){
    graph[from].push_back(Edge<T>(to, cost));
}

template<typename T>
T Prim<T>::Run(){
    using Pi = pair<T, int>;
    T total = 0;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    que.emplace(0, 0);
    while(!que.empty()){
        Pi p = que.top();
        que.pop();
        if(used[p.second]) continue;
        used[p.second] = true;
        total += p.first;
        for(auto &e : graph[p.second]){
            que.emplace(e.cost, e.to);
        }
    }
    return total;
}
/*************** using variables ***************/

/**********************************************/

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n;
    cin >> n;
    Prim<int> prim(n);
    REP(i, n){
        REP(j, n){
            int x;
            cin >> x;
            if(x != -1) prim.AddEdge(i, j, x);
        }
    }

    cout << prim.Run() << endl;
}