Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <algorithm>
#include <deque>
using namespace std;

#define INF 1e18
#define int long long

signed main() {
    int n; cin >> n;
    vector<vector<int> > adj(n, vector<int> (n));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            int a; cin >> a;
            adj[i][j] = (a == -1 ? INF : a);
        }
    }

    int u , minv;
    int unvisited = 0; int visited = 1; int fixed = 2;
    vector<int> dist(n, INF), parent(n, -1), status(n, unvisited);

    dist[0] = 0;

    while(1) {
        u = -1;
        minv = INF;
        for(int i = 0; i < n; i++) {
            if(minv > dist[i] && status[i] != fixed) {
                u = i;
                minv = dist[i];
            }
        }
        if(u == -1) break;
        status[u] = fixed;
        for(int v = 0; v < n; v++) {
            if(status[v] != fixed && adj[u][v] != INF) {
                if(dist[v] > adj[u][v]) {
                    dist[v] = adj[u][v];
                    parent[v] = u;
                    status[v] = visited;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0; i < n; i++) {
        sum += dist[i];
    }

    cout << sum << endl;
    return 0;
}

C++: #include <iostream>
#include <limits>
#define N 110

int main(){
  int n;
  std::cin >> n;
 
  int A[N][N];
  int d[N] = {};
  int p[N];
  for(int i=0; i<n; i++){
    d[i] = std::numeric_limits<int>::max();
    p[i] = -1;
    for(int j=0; j<n; j++){
      std::cin >> A[i][j];
      if( A[i][j] == -1){ //no line
        A[i][j] = std::numeric_limits<int>::max();
      }
    }
  }
  d[0] = 0;

  int used[N] = {};
  while(1){
    int u = -1;
    int min = std::numeric_limits<int>::max();
    for(int i=0; i<n; i++){
      if( used[i] != -1 && d[i]<min ){
        min = d[i];
        u = i;
      }
    }
    if(min==std::numeric_limits<int>::max()) break;
    used[u] = -1;

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

  std::cout << MST << std::endl;

  return 0;
}

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

int root(int i,vector<int> &parent){
    if(parent[i]==-1){
        //親がいない時
        return i;
    }else{
        int result=root(parent[i],parent);
        parent[i]=result; //パス圧縮
        return result;
    }
}

void unite(int x,int y,vector<int> &parent){
    if(root(x,parent)!=root(y,parent)){
        parent[root(y,parent)]=root(x,parent);
    }
}

bool issame(int x,int y,vector<int> &parent){
    return root(x,parent)==root(y,parent);
}

int main(){
    int n;
    cin >> n;
    //first:cost,second:(first:from,second:to)
    vector<pair<int,pair<int,int> > > bridges(n*n);

    //入力
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            int cost;
            if(cin >> cost,cost!=-1){
                bridges.push_back(pair<int,pair<int,int> >(cost,pair<int,int>(i,j)));
            }
        }
    }

    //sort
    sort(bridges.begin(),bridges.end());

    //島を管理する用
    vector<int> parents(n*n,-1);

    int sum=0;

    for(int i=0;i<bridges.size();++i){
        if(!issame(bridges[i].second.first,bridges[i].second.second,parents)){
            unite(bridges[i].second.first,bridges[i].second.second,parents);
            sum+=bridges[i].first;
        }
    }
    cout << sum;
}

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

const int MAX=105;
int state[MAX],par[MAX],N,S[MAX][MAX];
priority_queue<pair<int,int>> Q;

int prim(){
    int sum=0,cnt=1;
    state[0]=1;
    for(int i=0;i<N;i++){
        if(S[0][i]!=10000) Q.push(make_pair(-S[0][i],i));
    }
    while(1){
        int a=Q.top().second;
        sum+=-Q.top().first;
        state[a]=1;
        Q.pop();
        cnt++;
        for(int i=0;i<N;i++){
            if(S[a][i]!=10000&&state[i]==0) Q.push(make_pair(-S[a][i],i));
        }
        if(cnt==N) break;
    }
    return sum;
}


int main(){

    cin>>N;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            int a;cin>>a;
            if(a==-1) S[i][j]=10000;
            else S[i][j]=a;
        }
    }
    cout<<prim()<<endl;
}

C++: #include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

#define N 100
#define INF 0x7FFFFFFF

int prim(std::unordered_map<int, int> a[N], const int n){
    std::unordered_set<int> checked;
    int total = 0;
    checked.insert(0);
    while(checked.size() != n){
        int newId = INF;
        int newWeight = INF;
        for(auto &x: checked){
            for(auto &j: a[x]){
                if(checked.find(j.first) == checked.end() && newWeight > j.second){
                    newId = j.first;
                    newWeight = j.second;
                }
            }
        }
        checked.insert(newId);
        total += newWeight;
    }
    return total;
}

int main(){
    int n;
    std::scanf("%d", &n);
    std::unordered_map<int, int> a[N];
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            int v;
            std::scanf("%d", &v);
            if(v != -1){
                a[i].insert(std::make_pair(j, v));
            }
        }
    }
    int val = prim(a, n);
    std::printf("%d\n", val);
    return 0;
}