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 <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Edge {
    ll s,t;
    ll w;
    bool operator < (const Edge& tgt) const {
	return w > tgt.w;
    }
    void dump(){
	cout << s << " --> " << t << " : " << w << endl; 
    }
};

ll Prim(const vector<vector<Edge>>& G) {
    const ll V = G.size();
    priority_queue<Edge> edges;
    
    vector<int> used(V,0);
    //??????0??????????????????
    used[0] = 1;
    for(auto& e : G[0]) edges.push(e);

    ll ans = 0;
    while( !edges.empty() ) {
	Edge e = edges.top(); edges.pop();

	assert(used[e.s] == 1);
	if( used[e.t] == 1 ) continue;
	used[e.t] = 1;
//	e.dump();
	ans += e.w;

	for(auto& nxt : G[e.t]) {
	    if( used[nxt.t] == 1 ) continue;
	    edges.push(nxt);
	}
    }
    for(auto& e : used) assert(e == 1);

    return ans;
}


int main () {

    ll V;
    cin >> V;
    vector<vector<Edge>> G(V);

    for(int i=0; i<V; i++) {
	for(int j=0; j<V; j++) {
	    ll w; cin >> w;
	    if( w != -1 and w != 0) {
		G[i].push_back( Edge{i,j,w} );
//		G[j].push_back( Edge{j,i,w} );
//		cout << i << " <--> " << j << " : " << w << endl; 
	    }
	}
    }

    ll ans = Prim(G);
    cout << ans << endl;
        
    return 0;
}

C++: #include <iostream> 
#include<vector>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<queue>
#include<deque>
#include<iomanip>
#include<sstream>
#include<cmath>
#include<bitset>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(int i = 1;i <=  n; i++)
#define rrep(i,n) for(int i = n -1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
typedef long long int ll;
typedef long double ld;
const ll INF = (1 << 29);
ll h, m, n; string s;
const ll MOD= 1000000007;
const ld PI = acos(-1);
ll sum = 0;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
    std::fill((T*)array, (T*)(array + N), val);
}int a[110][101];
int prim() {
    vector<int> d(110, INF), p(110, -1),co(110,0);
    d[0] = 0; int u, miv = 0;
    while (1) {
        miv = INF;
        u = -1;
        rep(i, n) {
            if (miv > d[i] && co[i] != 2) {
                u = i; miv = d[i];
            }
        }
        if (u == -1) break;
        co[u] = 2;
        rep(v, n) {
            if (co[v] != 2 && a[u][v] != INF) {
                if (d[v] > a[u][v]) {
                    d[v] = a[u][v];
                    p[v] = u; co[v] = 1;
                }
            }
        }
    }
    rep(i, n) {
        if (p[i] != -1) sum += a[i][p[i]];
    }return sum;
}
int main() {
    cin >> n; 
    rep(i, n) {
        rep(j, n) {
            cin >> a[i][j];
            if (a[i][j] == -1) {
                a[i][j] = INF;
            }
        }
    }
    cout << prim() << endl;
}

C++: #include <iostream>
#include <stdio.h>
using namespace std;

#define MAX 100
#define INF 2097152
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];

int main(void){

	int i,j,f;
	int n,u,v,minv,d[MAX],p[MAX],color[MAX],sum=0;
	scanf("%d",&n);
  
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			scanf("%d",&f);
			if(f == -1){
				M[i][j] = INF;
			}else{
				M[i][j] = f;
			}
		}
	}


	for(i=0;i<n;i++){
		d[i] = INF;
		p[i] = -1;
		color[i] = WHITE;
	}

	d[0] = 0;

	while(1){
		minv = INF;
		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 && M[u][v] != INF){
				if(d[v] > M[u][v]){
					d[v] = M[u][v];
					p[v] = u;
					color[v] = GRAY;
				}
			}
		}
	}

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

	printf("%d\n",sum);


return 0;

}

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

const int MAX = 100;

int count(bool check[MAX], int graph[MAX][MAX], int n,int i) {
	check[i] = true;
	int sum = 0;
	for (int j = 0; j < n; j++) {
		if (graph[i][j] != -1 && check[j] == false) {
			sum += count(check,graph, n, j);
			sum += graph[i][j];
		}
	}
	return sum;
}

int main() {
	int n;
	int g;
	int graph[MAX][MAX];

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

	//?§????
	for (int s = 0; s < n; s++) {
		//?????±???
		for (int m = 0; m < n; m++) {
			if (graph[s][m] == -1)continue;
			//??????
			for (int e = 0; e < n; e++) {
				if (graph[s][e] == -1 || graph[m][e] == -1)continue;
				if (graph[s][e] > graph[s][m] + graph[m][e]) {
					graph[s][e] = -1;
				}
			}
		}
	}

	bool check[MAX] = { false };
	cout << count(check, graph, n, 0);
	return 0;
}

C++: #include<stdio.h>
  
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define NIL -1
#define INF 100000

int n,G[MAX][MAX];

int prim(){
  int u,mincost,i;
  int d[MAX],pi[MAX],color[MAX];
  
  for( u = 0 ; u < n ; u++ ){
    d[u] = INF;
    pi[u] = NIL;
    color[u] = WHITE;
  }
  
  d[0] = 0;
  
  while(1){
    mincost = INF;
    i = NIL;
    for( u = 0 ; u < n ; u++ ){
      if(color[u] != BLACK && d[u] < mincost){
    i = u;
    mincost = d[u];
      }
    }
  
    if( i == NIL ) break;
  
    color[i] = BLACK;
  
    for( u = 0 ; u < n ; u++ ){
      if(color[u] != BLACK && G[i][u] != INF ){
    if( d[u] > G[i][u] ){
      d[u] = G[i][u];
      pi[u] = i;
      color[u] = GRAY;
    }
      }
    }
  }
  
  int sum = 0;
  
  for( u = 0 ; u < n ; u++ ){
    if( pi[u] != NIL ) sum += G[u][pi[u]];
  }
  
  return sum;
} 

  
int main(){
  int i,j,m;
  scanf("%d",&n);
  
  for( i = 0 ; i < n ; i++ ){
    for( j = 0 ; j < n ; j++ ){
      scanf("%d",&m);
      if(m==NIL) G[i][j]=INF;
      else G[i][j]=m; 
    }
  }
  
  printf("%d\n",prim());
  
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#define PII pair<int,int>
#define MPII(a,b) make_pair(a,b)
using namespace std;
class NODE
{
public:
	vector<PII> v;
	int index = 0;
};
class Tree
{
public:
	vector<int> V;
	int cost = 0;
};

int main()
{
	NODE* nodes;
	nodes = new NODE[101];
	Tree T;
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			int t;
			cin >> t;
			if (t != -1)
			{
				PII p = MPII(t, j);
				nodes[i].v.push_back(p);
			}
		}
		sort(nodes[i].v.begin(), nodes[i].v.end());
	}
	T.V.push_back(1);
	while (1)
	{
		if (T.V.size() == n+1)break;
		int mincost=10000;
		int addv=-1;
		int index;
		for (auto itr : T.V)
		{
			if (mincost >= nodes[itr].v[nodes[itr].index].first)
			{
				mincost = nodes[itr].v[nodes[itr].index].first;
				addv = nodes[itr].v[nodes[itr].index].second;
				index = itr;
			}
		}
		nodes[index].index++;
		T.V.push_back(addv);
		T.cost += mincost;
	}
	cout << T.cost << endl;
	delete[] nodes;
	return 0;
}