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++: /*
 * a.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 100;

/* typedef */

struct Edge {
  int i, j, c;
  Edge() {}
  Edge(int _i, int _j, int _c): i(_i), j(_j), c(_c) {}
  bool operator<(const Edge &e) const { return c < e.c; }
};

typedef vector<Edge> ve;

struct UFT {
  int links[MAX_N], ranks[MAX_N], sizes[MAX_N];

  void clear(int n) {
    for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1;
  }
  UFT() {}
  UFT(int n) { clear(n); }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

/* subroutines */

/* main */

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

  ve es;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int ai;
      cin >> ai;
      if (ai >= 0) es.push_back(Edge(i, j, ai));
    }
  sort(es.begin(), es.end());

  UFT uft(n);
  int en = es.size(), sum = 0;

  for (int i = 0; i < en; i++) {
    Edge &ei = es[i];
    if (! uft.same(ei.i, ei.j)) {
      uft.merge(ei.i, ei.j);
      sum += ei.c;
    }
  }

  printf("%d\n", sum);
  return 0;
}

C++: // プリム法 最小全域木 Minimum Spanning Tree
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
static const int MAX = 100;
static const int INF = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX]; //M:隣接行列

int prim() {
    int u, minv;
    int distance[MAX], parent[MAX], color[MAX];

    for(int i = 0; i < n; i++) {
        distance[i] = INF;
        parent[i] = -1;
        color[i] = WHITE;
    }

    distance[0] = 0;

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

        if (u == -1)
            break;

        color[u] = BLACK;
        for(int v = 0; v < n; v++) {
            if (color[v] != BLACK && M[u][v] != INF) {
                if (distance[v] > M[u][v]) {
                    distance[v] = M[u][v];
                    parent[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }

    int sum = 0;
    for(int i = 0; i < n; i++) {
        if (parent[i] != -1)
            sum  += M[i][parent[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) ? INF : e;
        }
    }

    cout << prim() << endl;
    
    return 0;
}

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

using namespace std;

// calculate Minimum Spanning Tree with Prim's algorithm
void mst(vector<vector<int>>& g, vector<int>& weights) {
    int n_vertex = weights.size();
    vector<bool> mst(n_vertex, false);

    weights[0] = 0;
    while (true) {
        int idx;
        int min_weight = INT_MAX;
        for (int i = 0; i < n_vertex; ++i) {
            if (!mst[i] && weights[i] < min_weight) {
                idx = i;
                min_weight = weights[i];
            }
        }

        if (min_weight == INT_MAX)
            break;

        mst[idx] = true;

        for (int i = 0; i < n_vertex; ++i) {
            if (!mst[i] && g[idx][i] != -1)
                weights[i] = min(weights[i], g[idx][i]);
        }
    }
}

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

    vector<vector<int>> g(n, vector<int>(n, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
            cin >> g[i][j];
    }

    vector<int> weights(n, INT_MAX);
    mst(g, weights);

    int sum_of_weights = 0;
    for (int i = 0; i < n; ++i)
        sum_of_weights += weights[i];
    cout << sum_of_weights << '\n';
}

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

const int N = 100;
int n;
int A[N][N];
bool visit[N] = {0};
priority_queue< pair<int, int> > Q;

int main()
{
	cin >> n;
	for (int i=0; i<n; i++)
	{
		for (int j=0; j<n; j++)
		{
			cin >> A[i][j];
		}
	}

	Q.push(pair<int, int>(0,0));

	int cost = 0;

	while (Q.size())
	{
		pair<int, int> q = Q.top();
		Q.pop();
		int k = q.second;
		if (visit[k]) continue;
		cost += q.first;
		visit[k]=true;
		for(int j=0;j<n;j++)
		{
			int c = A[k][j];
			if (c <= 0) continue;
			if (visit[j]) continue;
			Q.push(pair<int, int>(-c,j));
		}
	}

	cout << -cost << endl;
		
	return 0;
}