Go: package main

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

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0

	for {
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			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 && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += g[i][p[i]]
		}
	}

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <iostream>
using namespace std;
static const int MAX = 100, INFTY = (1 << 21), WHITE = 0, GRAY = 1, BLACK = 2;

int N, M[MAX][MAX];

int prim() {
	int u, minv;
	int D[MAX], P[MAX], C[MAX];

	for (int i = 0; i < N; i++) {
		D[i] = INFTY;
		P[i] = -1;
		C[i] = WHITE;
	}

	D[0] = 0;
	while (true) {
		minv = INFTY;
		u = -1;
		for (int i = 0; i < N; i++) {
			if (minv > D[i] && C[i] != BLACK) {
				u = i;
				minv = D[i];
			}
		}
		if (u == -1) break;
		C[u] = BLACK;
		for (int v = 0; v < N; v++) {
			if (C[v] != BLACK && M[u][v] != INFTY) {
				if (D[v] > M[u][v]) {
					D[v] = M[u][v];
					P[v] = u;
					C[v] = GRAY;
				}
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < N; i++) {
		if (P[i] != -1) sum += M[i][P[i]];
	}
	return sum;
}

int main() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			int e; cin >> e;
			M[i][j] = (e == -1) ? INFTY : e;
		}
	}
	cout << prim() << endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
#define INFTY 1000000
using namespace std;

const int N = 100;
int n;
int D[N], M[N][N];
bool V[N];

int prim() {
  int nn, minD, cost;

  REP(i, n) D[i] = INFTY;
  D[0] = 0;

  while (true) {
    minD = INFTY;
    REP(i, n) {
      if (!V[i] && D[i] < minD) {
        minD = D[i];
        nn = i;
      }
    }
    if (minD == INFTY) break;

    cost += D[nn];
    V[nn] = true;
    REP(i, n) {
      if (!V[i] && M[nn][i] != -1 && M[nn][i] < D[i]) {
        D[i] = M[nn][i];
      }
    }
  }

  return cost;
}

int main() {
	cin.tie(0);
 	ios::sync_with_stdio(false);

  cin >> n;
  REP(i, n) {
    REP(j, n) {
      cin >> M[i][j];
    }
  }

  cout << prim() << endl;
}

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

struct UF{
   vector<int> par;
   vector<int> sz;
   UF(){}
   UF(int n){
       par.resize(n);
       sz.resize(n, 1);
       for(int i = 0; i < n; i++) par[i] = i;
   }
   int find(int x){
       if(x == par[x]) return x;
       return par[x] = find(par[x]);
   }
   void unite(int x, int y){
       x = find(x); y = find(y);
       if(x == y) return;
       if(sz[x] < sz[y]) swap(x, y);
       sz[x] += sz[y];
       par[y] = x;
   }
   bool same(int x, int y){
       return find(x) == find(y);
   }
};

struct edge{
    int u, v, cost;
    bool operator<(const edge &e) const{
        return cost < e.cost;
    }
};

int n, m;
vector<edge> es; 

int kruskal(){
    int res = 0;    
    UF uf(n);
    sort(es.begin(), es.end());
    for(int i = 0; i < m; i++){
        edge e = es[i];
        if(!uf.same(e.u, e.v)){
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main(){
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int in;
            cin >> in;
            if(in == -1) continue;
            es.push_back({i, j, in});
        }
    }
    m = es.size();
    cout << kruskal() << endl;
}

C++: # include <iostream>
using namespace std;
const int MAXN = 1000;
int main(void)
{
	int d[MAXN],a[MAXN][MAXN],vis[MAXN],p[MAXN];
	int n,ans = 0,cnt = 0;
	scanf("%d",&n);
	for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) scanf("%d",&a[i][j]);
	for(int i = 0;i < n;i++)
	{
		vis[i] = 0;
		p[i] = -1;
		d[i] = 2001;
	}
	d[0] = 0;
	while(true)
	{
		int u = -1,minv = 2001;
		for(int i = 0;i < n;i++)
		{
			if(d[i] < minv && !vis[i])
			{
				u = i;
				minv = d[i];
			}	
		}
		if(u == -1) break;
		cnt++;
		vis[u] = 1;
		for(int v = 0;v < n;v++)
		{
			if(!vis[v] && a[u][v] != -1)
			{
				if(d[v] > a[u][v])
				{
					d[v] = a[u][v];
					p[v] = u;
				}
			}
		}
	}
	for(int i = 0;i < n;i++) if(p[i] != -1) ans += a[i][p[i]];
	printf("%d\n",(cnt == n)?ans:-1);
	return 0;
}

C++: #if 0
????????£????????????1.5h
??¢?????????????????§??????????????¢??°????????£????????????sum????¨??????¨????????????????????£????????§?????????
???????????¢??????????????§????????????????????????????????????"-1"???????????????????????????????????§???????????§????????¨???????°???????????????????
#endif

#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int P[10000+10];

void init(int N){
	for(int i=0;i<=N;++i) P[i]=i;
}
int root(int a){
	if(P[a]==a)return a;
	else return(P[a]=root(P[a]));
}
bool is_same_set(int a,int b){
	return root(a)==root(b);
}
void unite(int a,int b){
	P[root(a)]=root(b);
}
int main(){
	int N;
	cin>>N;
	int A[N][N];
	
	for(int i=0; i<N;i++){
		for(int j=0;j<N;j++){
			cin>>A[j][i]; //???????????\???
		}
	}

	int M = 0; 
	int cost[N*N],src[N*N],dst[N*N];
	for (int r=0; r<N;r++)
		for (int c=0; c<r;c++) //r<c???????§?????????????
			if (A[r][c] > -1) {
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				M += 1;
			}
	pair<int,int> edges[2000+10];
	for(int i=0;i<M;i++)
		edges[i]=make_pair(cost[i],i); //first???????????????second???ID
	sort(edges,edges+M);
	
	//for(int i=0;i<M;++i)
		//cout<<edges[i].first<<endl;

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

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define REPR(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

using Graph = vector<vector<int> >;

int n, sum;
vector<bool> seen;
priority_queue<pair<int, int> > pq;

void dfs(const Graph &G, int v, int p){
  seen[v] = true;

  rep(u, n){
    if(u == p) continue;
    if(G[v][u] == -1) continue;
    pq.push(make_pair((-1)*G[v][u], u));
  }

  while(!pq.empty()){
    pair<int, int> tmp;
    tmp = pq.top();
    pq.pop();
    if(seen[tmp.second]) continue;
    sum += (-1)*tmp.first;
    int nv = tmp.second;
    p = v;
    dfs(G, nv, p);
  }

  return;
}

int main(){
  cin >> n;
  Graph G(n, vector<int>(n));
  rep(i, n) rep(j, n) cin >> G[i][j];

  seen.assign(n, false);
  sum = 0;
  dfs(G, 0, -1);

  cout << sum << endl;

  return 0;
}