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++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Edge {
  int weight;
  int source;
  int destination;
};

int main() {
  int n;
  cin >> n;
  auto cmp = [](const Edge& lhs, const Edge& rhs) {
    return lhs.weight > rhs.weight;
  };
  vector<vector<Edge>> edges(n, vector<Edge>());
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      int w;
      cin >> w;
      if (w != -1) {
        Edge e {w, i, j};
        edges[i].push_back(move(e));
      }
    }
  }

  int total_weight = 0;
  unordered_set<int> contained;
  contained.insert(0);
  priority_queue<Edge, vector<Edge>, decltype(cmp)> q(cmp);
  for (auto&& e : edges[0]) {
    q.push(e);
  }
  while (static_cast<int>(contained.size()) < n) {
    auto e = q.top();
    q.pop();
    if (contained.find(e.destination) == contained.end()) {
      total_weight += e.weight;
      contained.insert(e.destination);
      for (auto&& edge : edges[e.destination]) {
        q.push(edge);
      }
    }
  }
  cout << total_weight << endl;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,int> Info;
int main(){
        int N;
        cin >> N;
        vector <vector<int> > adjancey(N,vector<int>(N));
        priority_queue<Info, vector<Info>,greater<Info> > descending;
        for(int i=0 ; i<N ; i++ )
                for(int j=0 ; j<N ; j++ )
                        cin >> adjancey[i][j];
        Info wn;
        int now=0,counter=0;
        vector <bool> past(N,false);
        while(true){
                past[now]=true;
                for(int i=0 ; i<N ; i++ ){
                        if(past[i])
                                continue;
                        if(adjancey[now][i]==-1)
                                continue;
                        wn.first=adjancey[now][i];
                        wn.second=i;
                        descending.push(wn);
                }
                wn=descending.top();descending.pop();
                now=wn.second;
                if(past[now])break;
                counter+=wn.first;
        }
        cout << counter << endl;
        return 0;
}

C++: #include <stdio.h>

#define MAX 101
#define INF 100000
#define FLAG 1
#define DEFLAG 0

int main(){
  int i,j,n,min,a[MAX][MAX],k,x[MAX],minNode,sum=0,g[MAX];
  
  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&a[i][j]);
    }
  }

  //calc
  k = 1;
  x[0] = 0;
  g[0] = FLAG;
  for(i=1;i<n;i++){
    g[i] = DEFLAG;
  }
  
  while(1){
    min = INF;
    
    for(i=0;i<k;i++){
      for(j=0;j<n;j++){
	if(min > a[x[i]][j] && a[x[i]][j]!= -1 && g[j] != FLAG){
	  min = a[x[i]][j];
	  minNode = j;
	}
      }
    }

    if(k!=n){
      x[k] = minNode;
      g[minNode] = FLAG;
      k++;
      sum += min;
    }else{ break; }
    
  }

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

C++: #include <iostream>
#include <vector>
#include <numeric>
#include <limits>

using namespace std;

const int N = 100;

enum {
    WHITE,
    GRAY,
    BLACK,
};

int main() {
    int A[N][N];

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int a;
            cin >> a;
            A[i][j] = a == -1 ? a : numeric_limits<int>::max();
        }
    }

    // ????????????
    vector<int> color(n, WHITE); // ??????????¨??????¶???
    vector<int> d(n, numeric_limits<int>::max());
    vector<int> p(n, -1);

    d[0] = 0;
    while (1) {
        int minv = numeric_limits<int>::min();
        int 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 && A[u][v] != numeric_limits<int>::max()) {
                if (d[v] > A[u][v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum;
    for (int i =0; i < n; ++i) {
        if (p[i] != -1)
            sum += A[i][p[i]];
    }
    cout << sum << endl;

    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 2000000000;
int main() {
	int n; cin >> n;
	int m[101][101];
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
		cin >> m[i][j];
	int res = 0;
	int *d = new int[n];
	fill(d, d + n, INF); 
	d[0] = 0;
	bool *visit = new bool [n];
	priority_queue <P, vector<P>, greater<P> > q;
	q.push(P(d[0], 0));
	while (!q.empty()) {
		int f = q.top().first, s = q.top().second;
		//cout << "f " << f << " s " << s << endl;
		visit[s] = true;
		res += f;
		q.pop();
		for (int i = 0; i < n; i++) {
			if (i == 1) {
				//cout << !visit[i] << " " << m[s][i] << " " << d[i] << endl;
			}
			if (!visit[i] && m[s][i] != -1 && m[s][i] < d[i]) {
				d[i] = m[s][i];
				//cout << "i " << i << " d " << d[i] << endl;
				q.push(P(d[i], i));
			}
		}
	}
	cout << res << endl;
	delete[] d, visit;
	return 0;
}