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 "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)
# pragma warning(disable:4996)

int V;
int E;
struct edge { int from, to, cost; };

typedef pair<int, int> PP;

vector<edge> e;

int Par[100000];
int Rank[100000];

void init(int n) {
	for (int i = 0; i < n; i++) {
		Par[i] = i;
		Rank[i] = 0;
	}
}

int find(int x) {
	if (Par[x] == x) {
		return x;
	}
	else {
		return Par[x] = find(Par[x]);
	}
}

void unite(int  x, int y) {
	x = find(x);
	y = find(y);
	if (x == y) return;
	if (Rank[x] < Rank[y]) {
		Par[x] = y;
	}
	else {
		Par[y] = x;
		if (Rank[x] == Rank[y])Rank[x]++;
	}
}

bool same(int x, int y) {
	return find(x) == find(y);
}

// ソート時に比較するための関数 
bool comp(const edge& e1, const edge& e2) {
	return e1.cost < e2.cost;
}

int kruskal() {
	init(V);
	sort(e.begin(), e.end(), comp);
	int ans = 0;
	for (int i = 0; i<E; i++) {
		if (!same(e[i].from, e[i].to)) {
			unite(e[i].from, e[i].to);
			ans += e[i].cost;
		}
	}
	return ans;
}

int main() {
	cin >> V;
	REP(i, V)REP(j, V) {
		int num;
		cin >> num;
		e.emplace_back(edge{i, j, num});
		E++;
	}
	cout << kruskal() << endl;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef pair<int, int> P;
int n, ans, n_left, dis[101];
vector< vector<P> > G(101);
priority_queue< P, vector<P>, greater<P> > heap;
bool visited[101];

int main(){
   

    int u, v, w;
    scanf("%d", &n);
    n_left = n;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            scanf("%d", &w);
            if(w==-1) continue;
            G[i].push_back(P(w, j));
        }
    }
    ans = 0;
    memset(visited, 0, sizeof(visited));
    memset(dis, 127, sizeof(dis));
    heap.push(P(0, 0));
    dis[0] = 0;
    
    while(!heap.empty() && n_left>0){
        P top = heap.top();
        heap.pop();
        u = top.second;

        if(visited[u]) continue;
        visited[u] = true;
        --n_left;
        ans += top.first;
        for(int i=0; i<G[u].size(); i++){
            v = G[u][i].second;
            if(visited[v]) continue;
            w = G[u][i].first;
            if(w<dis[v]){
                dis[v] = w;
                heap.push(P(w, v));
            }
        }
    }

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

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

const int INF=99999999;


void get_network(int N,vector<vector<int>>& A){
    int s;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
                cin>>s;
                A[i][j]=s;
        }
    }
}

int main(){
        int n;
        scanf("%d\n",&n);
        vector<vector<int>> array(n,vector<int>(n,0));
        get_network(n,array);

        // flag
        vector<int> flag(n,-1);
        // parent nodes
        vector<int> p(n,0);
        // mincost
        vector<int> d(n,INF);

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

        int mincost,u;
        while(true){
                // search mincost
                mincost=INF;
                for(int i=0;i<n;i++){
                        if(flag[i]!=1 && d[i]<mincost){
                                mincost=d[i];
                                u=i;
                        }
                }

                if(mincost==INF)break;

                flag[u]=1;

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

        int w=0;
        for(int i=0;i<p.size();i++){
                if(p[i]!=-1){
                        w+=array[p[i]][i];
                }
        }
        printf("%d\n",w);


        return 0;
}

C++: #include<iostream>
#include<algorithm>
#define MAX_E 10000
#define MAX_N 100
using namespace std;
void init_union_find(int);
bool same(int ,int);
void unite(int,int);
struct edge{
  int u,v,cost;
  bool operator < (const edge& e) const{
    if(cost != e.cost) return cost < e.cost;
    else if(u != e.u) return u < e.u;
    else return v < e.v;
  }
};

ostream& operator << (ostream& os,const edge& e){
  os <<e.cost<<e.u<<e.v;
}

int E,n;
edge G[MAX_E];

int A[100][100];

int kruskal(){ 
  sort(G,G + E);
  init_union_find(n);
  int res =0;
  for(int i = 0; i < E; i++){
    edge e = G[i];
    if(!same(e.u,e.v)){
       res += e.cost;
       unite(e.u,e.v);
    }
  }

  return res;
}


int main(){
  cin >> n;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> A[i][j];
      if(A[i][j]==-1){
	continue;
      }
      G[E]=(edge){i,j,A[i][j]};
      E++;
    }
  }
  cout <<kruskal()<<endl;
}




int par[MAX_N] ,rank[MAX_N];

void init_union_find(int n){
for(int i=0;i<n;i++) {
    par[i]=i;
    rank[i]=0;
 }
}

int find(int x){
  if ( par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x,int y){
  x = find(x);
  y = find(y);
  if(x == y) return;

  if(rank[x] < rank[y]){
    par[x] = y;
  }else {
    par[y] = x;
    if( rank[x] == rank[y]) rank[x]++;
  }
}
bool same(int x,int y){
  return find(x) == find(y);
}