Go: package main

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

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: //include
//------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <limits>
using namespace std;

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

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//debug
//-------------------------------------------
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define INF std::numeric_limits<int>::max()

//typedef
//-------------------------------------------
typedef long long ll;

int n;
int g[101][101];

int prim(int used, int sum) {
    if (used == 0) return sum;

    int tmp = INF;
    int use = -1;
    REP(i, n) {
        if ((used>>i)&1) {
            continue;
        }
        REP(j, n) {
            if (((used>>j)&1) && g[i][j] < tmp) {
                tmp = g[i][j];
                use = j;
            }
        }
    }
    return prim(used&~(1<<use), sum + tmp);
}

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

    cin >> n;
    REP(i, n) REP(j, n) {
        cin >> g[i][j];
        if (g[i][j] < 0) g[i][j] = INF;
    }
    int used = (1<<n) - 1;
    cout << prim(used&~1, 0) << endl;

    return 0;
}

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 <stdio.h>


int n,M[101][101];

int prim(void){
  int u,minv,i,n,sum=0,v;
  int d[101],p[101],color[101];
  
  for(i=0;i<n;i++){
    d[i]=10000000;
    p[i]=-1;
    color[i]=0;
  }
  
  d[0]=0;
  while(1){
    minv=10000000;
    u=-1;
    for(i=0;i<n;i++){
      if(minv>d[i] && color[i]!=2){
	u=i;
	minv=d[i];
      }
    }
    
    if(u==-1) break;
    color[u]=2;
    for(v=0;v<n;v++){
      if(color[v]!=2 && M[u][v]!=10000000){
	if(d[v]>M[u][v]){
	  d[v]=M[u][v];
	  p[v]=u;
	  color[v]=1;
	}
      }
    }
  }
  for(i=0;i<n;i++){
    if(p[i]!=-1) sum+=M[i][p[i]];
  }
  return sum;
}



int main(){
  int e,i,j,k;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&e);
      if(e==-1) M[i][j]=10000000;
      else M[i][j]=e;
    }
  }
  k=prim();
  printf("%d\n",k);
  return 0;  
}

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXSIZE = 1e2+7;
const int INF = 1e8+7;
int edge[MAXSIZE][MAXSIZE];
int dis[MAXSIZE];
bool vis[MAXSIZE];
int n;
int prim(){
	memset(vis,false,sizeof(vis));
	for(int i = 1; i <= n; i++){
		dis[i] = edge[1][i];
	}
	vis[1] = true;
	dis[1] = 0;
	int minc, k;
	for(int i = 2; i <= n; i++){
		minc = INF;
		for(int j = 1; j <= n; j++){
			if(!vis[j] && dis[j] < minc){
				minc = dis[j];
				k = j;
			}
		}
		if(minc == INF) break;
		vis[k] = true;
		for(int j = 1; j <= n; j++){
			if(!vis[j] && dis[j] > edge[k][j]){
				dis[j] = edge[k][j];
			}
		}
	}
	int cnt = 0;
	for(int i = 1; i <= n; i++) cnt += dis[i];
	return cnt;
}
int main(){
	while(~scanf("%d",&n)){
		memset(edge,0,sizeof(edge));
		int t;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				scanf("%d",&t);
				if(i == j) continue;
				if(t == -1) edge[i][j] = INF;
				else edge[i][j] = t;
			}
		}
		int res = prim();
		printf("%d\n",res); 
	}
	return 0;
}