Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			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 && M[u][v] != INFTY {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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 <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;

const int MAX = 105;
const int INF = 1000000007;
const int white = 0;
const int gray = 1;
const int black = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
  rep(i, n) {
    d[i] = INF;
    p[i] = -1;
    color[i] = white;
  }
  d[0] = 0;
  while (1) {
    minv = INF;
    u = -1;
    rep(i, n) {
      if (minv > d[i] && color[i] != black) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1) break;
    color[u] = black;
    rep(v, n) {
      if (color[v] != black && M[u][v] != INF) {
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = gray;
        }
      }
    }
  }
  int sum = 0;
  rep(i, n) {
    if (p[i] != -1) {
      sum += M[i][p[i]];
    }
  }
  return sum;
}

int main() {
  cin >> n;
  rep(i, n) {
    rep(j, n) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INF : e;
    }
  }
  cout << prim() << endl;

  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
constexpr int INF = (1 << 30) - 1;

int n;
int dist[105][105];

int prim()
{
    int d[105];
    bool flag[105] = {};
    int cnt = 0, sum = 0;
    fill_n(d, 105, INF);
    d[0] = 0;
    
    while ( true ) {
        int u = -1;
        for ( int i = 0; i < n; i++ ) {
            if ( !flag[i] && (u == -1 || d[u] > d[i] ) ) u = i;
        }
        
        flag[u] = true;
        sum += d[u];
        cnt++;
        if ( cnt == n ) break;

        for ( int i = 0; i < n; i++ ) {
            if ( !flag[i] && dist[u][i] != -1 && d[i] > dist[u][i] ) {
                d[i] = dist[u][i];
            }
        }
    }

    return ( sum );
}

int main()
{
    cin >> n;
    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; j < n; j++ ) {
            cin >> dist[i][j];
        }
    }
    cout << prim() << endl;
    return ( 0 );
}

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

#define WHITE 1
#define BLACK 2
#define GRAY 3
#define M 100
#define INFTY (1<<21)

int n,color[M],A[M][M];

int prim(){
  int d[M],p[M],u = -1,min;

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

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

    color[u] = BLACK;

    for(int i = 0;i < n;i++){
      if(color[i] != BLACK && A[u][i] != -1){
        if(A[u][i] < d[i]){
          d[i] = A[u][i];
          p[i] = u;
          color[i] = GRAY;
        }
      }
    }
  }

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

  return re;
}

int main(){

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

  cout << prim() << endl;

  return 0;
}