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 <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;
}