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 <iostream>
#include <stack>
#define N 100
#define WHITE 0
#define GREY 1
#define BLACK 2
#define INFTY 100000
using namespace std;

int color[N];//color用来记录访问状态
int M[N][N];//M[N][N]是图的邻接矩阵表示法
int d[N],p[N];
//d[N]用来记录连接T内顶点与V-T内顶点的边中，权值最小的边的权值
//p[n]用来记录MST中顶点v的父节点
void init_color_d()
{
    for(int i = 0; i < N; i++)
    {
        color[i] = WHITE;
        d[i] = INFTY;
    }
}
int prim(int n)
{
    init_color_d();
    d[0] = 0;//起始点为0结点，距离设置为0
    p[0] = -1;
    int u;
    while(true)//访问结点为u
    {
        int mincost = INFTY;
        for(int i = 0; i < n; i++)
        {
            if(color[i]!=BLACK&&d[i]<mincost)//求最小值
            {
                mincost = d[i];
                u = i;
            }
        }
        if(mincost==INFTY)break;//这时候表示没有取到最小值，即最小生成树已经完成
        color[u] = BLACK;//因为u是最小值，那么就先标志为访问完
        for(int v = 0; v < n; v++)
        {
            if(color[v]!=BLACK&&M[u][v]!=-1)//如果v没有被选入到MST集合里面，并且此边存在
            {
                if(M[u][v]<d[v])
                {
                    d[v] = M[u][v];//d[v]记录最小值
                    p[v] = u;//记录父节点
                    color[v] = GREY;//设置为已经尝试过此结点
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0;i < n;i++){
        if(p[i]!=-1)sum+=M[i][p[i]];
    }
    return sum;
}

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

vector<int> P;

int root(int v){ //根を返す
    if(P[v] == v) return v;
    else return root(P[v]);
}

bool is_same_set(int a, int b){ // 同じならtrue
    return root(a) == root(b);
}

void unite(int a, int b){ // aの根をbの根にくっつける
    P[root(a)] = root(b);
}

int main(){
    int n; cin >> n;
    P.resize(n); //　Pは作りかけの森を想定する
    for(int i = 0; i < n; i++){
        P[i] = i;
    }
    vector<tuple<int, int, int>> vec; //<コスト,始点, 終点>を格納する配列
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int a; cin >> a;
            if(a >= 0 && i < j){
                vec.push_back(make_tuple(a, i, j));
            }
        }
    }
    sort(vec.begin(), vec.end()); //コスト順にソート
    int ans = 0;
    for(auto v: vec){
        int cost = get<0>(v), str = get<1>(v), end = get<2>(v);
        if(is_same_set(str, end)) continue; //繋がっている
        //もし繋がっていなければ
        ans += cost; // コストを足す
        unite(str, end); //つなげてあげる
    }
    cout << ans << endl;
}

C++: #include <iostream>
#include <utility>
#include <vector>
#include <algorithm>

using namespace std;

int P[10010];
void init(int N){
    for(int i=0;i<=N;i++)
        P[i] = i;
}
int root(int a){
    if(P[a]==a)
        return a;
    return(P[a] = root(P[a]));
}
bool is_same_set(int a, int b){
    return root(a) == root(b);
}
void unit(int a, int b){
    P[root(a)] = root(b);
}

int main(){
    int n; cin >> n;
    pair<int, pair<int,int> > a;
    vector< pair<int, pair<int,int> > > e;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            int x; cin >> x;
            if(x!=-1){
                e.push_back(make_pair(x, make_pair(i,j)));
            }
        }
    }
    sort(e.begin(),e.end());
    int ans = 0;
    init(n);
    for(vector< pair<int, pair<int,int> > >::iterator it = e.begin();it != e.end();it++){
        int s = it->second.first;
        int g = it->second.second;
        if(!is_same_set(s,g)){
            unit(s,g);
            ans += it->first;
        }
    }
    cout << ans << endl;
}

C++: #include <iostream>

using namespace std;

#define MAX 100
#define INFTY 9999999 
#define WHITE 0
#define GRAY 1
#define BLACK 2

int M[MAX][MAX];
int color[MAX];
int d[MAX];
int p[MAX];
int n;

int prim()
{
	int mincost;
	int u;

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

	d[0] = 0;
	p[0] = -1;

	while (true) {
		mincost = INFTY;

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


		if (mincost == INFTY) {
			break;
		}

		color[u] = BLACK;

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


	int sum = 0;

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

	return sum;

}



int main(void)
{



	cin >> n;

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

	cout << prim() << endl;

	return 0;
}

C++: #include <iostream>
#include <climits>
#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;
}