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 <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int G[10010][10010];
int cost[10010],src[10010],dst[10010];

int P[10010];
int root(int a);
int is_same_set(int a,int b);
void unite(int a,int b);

int main() {
	int N,a;
	cin >> N;
	for(int i=0; i<N;i++) {
		for(int j=0;j<N;j++) {
			cin >> a;
			G[i][j] = a;
		}
	}
	int hcon = 1;
	int M = 0;
	pair<int,int> edges[10010];

	for(int r=0;r<N;r++) {
		for (int c=0;c<hcon;c++) {
			if(G[r][c] > -1) {
				cost[M] = G[r][c];
				src[M] = r;
				dst[M] = c;
				edges[M] = make_pair(cost[M],M);
				M+=1;
			}
		}
		hcon++;
	}
	sort(edges,edges+M);



	for(int i=0;i<N;i++) P[i] = i;

	int answer = 0;
	for(int i=0;i<M;i++) {
		if(!is_same_set(src[edges[i].second],dst[edges[i].second])) {
			unite(src[edges[i].second],dst[edges[i].second]);
			answer += edges[i].first;
			//cout << answer << endl;
		}
	}
	cout << answer << endl;;
}

int root(int a) { 
	if (P[a] == a) return a; 
	return (P[a] = root(P[a])); 
}
int is_same_set(int a, int b) { 
	if(root(a) == root(b)) {
		return 1;
	} else {
		return 0;
	}
}
void unite(int a, int b) { 
	P[root(a)] = root(b);
}

C++: #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define N 110
#define INF 0x3f3f3f3f
using namespace std;
typedef struct {
    int w;
    int s;
    int e;
}Edge;
Edge edge[N*N];
int m[N][N];
int father[N];
int n;
int k = 0;  //edge的下标
int result = 0;
int findfather(int x){
    return x == father[x] ? x : findfather(father[x]);
}
int cmp(Edge a, Edge b){
    return a.w < b.w;
}
void Kruscal(){
    for(int u = 0; u < k; u++){
        int s = edge[u].s;
        int e = edge[u].e;
        if(findfather(s) != findfather(e)){
            father[findfather(e)] = s;
            result += edge[u].w;
        }
    }
    printf("%d\n", result);
}
int main(){

    scanf("%d", &n);
    for(int i = 1; i <= n; i++) father[i] = i;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            int b;
            scanf("%d", &b);
            m[i][j] = b;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n ;j++){
            if(m[i][j] != -1) {
                edge[k].w = m[i][j];
                edge[k].s = i;
                edge[k].e = j;
                k++;
            }
        }
    }
    sort(edge, edge+k, cmp);
    Kruscal();
    return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;

using pii = pair<int, int>;

struct Graph{
  int v_size;
  vector<vector<pii>> adj;
  
  Graph(const int v_size):v_size(v_size){
    this->adj.resize(v_size);
  }
  
  void add_edge(const int s, const int t, const int w){
    this->adj[s].emplace_back(pii(t, w));
  }
};

int mst(const Graph& g){
  constexpr int INF{INT_MAX};
  
  vector<int> d(g.v_size, INF);
  priority_queue<pii, vector<pii>, greater<pii>> pq;

  d[0] = 0;
  pq.push(pii(0, 0));

  int ans {0};
  
  while(!pq.empty()){
    auto cost {pq.top().first};
    auto v {pq.top().second};
    pq.pop();

    if(d[v] < cost) continue;

    ans += cost;
    d[v] = -INF;

    for(int i = 0; i < g.adj[v].size(); ++i){
      auto to {g.adj[v][i].first};
      auto next_cost {g.adj[v][i].second};

      if(d[to] > next_cost){
        d[to] = next_cost;
        pq.push(pii(next_cost, to));
      }
    }
  }

  return ans;
}

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

  int x;
  for(int i = 0; i < n; ++i){
    for(int j = 0; j < n; ++j){
      cin >> x;
      if(x != -1){
        g.add_edge(i, j, x);
      }
    }
  }

  cout << mst(g) << endl;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<20);
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++) {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    d[0] = 0;

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

        if (minv == INFTY) {
            break; // All nodes visited
        }

        color[u] = BLACK;
        for (int v=0; v<n; v++) {
            if (color[v]!=BLACK && M[u][v]<INFTY) {
                if (M[u][v]<d[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]];
        // }
        sum += d[i];
    }

    return sum;
}

int main() {
    cin >> n;
    int e;
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cin >> e;
            if (e!=-1) {
                M[i][j] = e;
            } else {
                M[i][j] = INFTY;
            }
        }
    }
 
    cout << prim() << endl;
    return 0;
}