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>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<20

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int cost[105][105];
int mincost[105];
bool used[105];

int prim (int n )
{
	memset (used, false, sizeof (used ) );
	rep (i, n ) mincost[i] = INF;

	mincost[0] = 0;
	int res = 0;

	while (true ){
		int v = -1;
		// X  に属さない頂点のうち X からの辺のコストが最小になる頂点を探す
		rep (u, n )
			if (!used[u] && (v == -1 || mincost[u] < mincost[v] ) ) v = u;

		if (v == -1 ) break;

		used[v] = true;
		res += mincost[v];

		rep (u, n ) mincost[u] = min (mincost[u], cost[v][u] );
	} // end while

	return res;
}

int main()
{
	memset (cost, -1, sizeof (cost ) );
	ios_base::sync_with_stdio(0);
	int n; cin >> n;
	rep (i, n ) rep (j, n ) cost[i][j] = INF;

	rep (i, n ) rep (j, n ){
		int in; cin >> in;
		if (in == -1 ) in = INF;
		cost[i][j] = in;
	} // end rep

	int res = prim(n );
	cout << res << endl;
		
	return 0;
}

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

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n;
vector<vector<int>> G;
vector<int> d;
vector<int> colors;

int mst(){
    int left = n;
    int res = 0;
    d[0] = 0;
    //int u = 0;
    while(left--){
        int min_id = -1;
        int cur_min = INT_MAX;
        for(int i = 0;i<n;i++){
            if(colors[i] !=BLACK && d[i] < cur_min){
                min_id = i;
                cur_min = d[i];
            }
        }
        if(min_id==-1) break;
        int u = min_id;
        //printf("%d\n",min_id);
        res += d[u];
        colors[u] = BLACK;
        for(int v =0;v<n;v++){
            if(colors[v]!=BLACK && G[u][v] < d[v]){
                d[v] = G[u][v];
            }
        }
    }
    return res;
}

int main()
{
    //freopen("data.txt","r",stdin);
    cin >> n;
    G.resize(n,vector<int>(n,INT_MAX));
    d.resize(n,INT_MAX);
    colors.resize(n,WHITE);
    int tmp;
    for(int i =0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> tmp;
            if(tmp!=-1) G[i][j] = tmp;
        }
    }
    cout << mst() <<endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define USED 1
using namespace std;
typedef pair<int, int> pi;
int graph[200][200];
int used[200];
// first:weight, second:key
priority_queue<pi, vector<pi>, greater<pi>> pqueue;

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> graph[i][j];   
    }
  }
  
 
  int ans = 0, key;
  pqueue.push(make_pair(0,0));
  for (int i = 0; i < n; i++) {
    pi p = pqueue.top();
    pqueue.pop();
    key = p.second;
    if (used[key] == USED) {
      i--;
      continue;
    }
    used[key] = USED;
    ans += p.first;
    //cout << "Distance: " << p.first << ", Key: " << p.second << endl;
    for (int j = 0; j < n; j++) {
      if (graph[key][j] != -1 && used[j] != USED) {
        pqueue.push(make_pair(graph[key][j],j));
      }
    }
  }
  cout << ans << endl;
}

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

int main(){
  long n;
  long G[100][100],d[100];
  long minc,mi,i;
  bool flg[i];
  long sum=0;
  
  cin >> n;
  for (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;
    }
    d[i]=INF;
    flg[i]=false;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (!flg[i]&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flg[mi]=true;
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (!flg[i]&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    sum+=d[i];
  }
  cout << sum<<endl;
  
  return 0;
}