Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

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

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

C++: #include <cstdio>
#include <vector>

int main(void){
	int n, min_edge, next_node, count, sum;
	scanf("%d", &n);
	std::vector<std::vector<int>> edges;
	std::vector<int> isUsed;
	edges.resize(n);
	isUsed.resize(n);
	for(int i=0;i<n;i++){
		isUsed[i]=-1;
		edges[i].resize(n);
		for(int j=0;j<n;j++){
			scanf("%d", &edges[i][j]);
		}
	}

	min_edge=3000;
	isUsed[0] = 1;
	count = 0;
	sum=0;
	while(count<4){
		for(int i=0;i<n;i++){
			if(isUsed[i]==1){
				for(int j=0;j<n;j++){
					if (isUsed[j]==-1 && edges[i][j]>-1 && edges[i][j]<min_edge){
						min_edge = edges[i][j];
						next_node = j;
					}
				}
			}
		}
		sum += min_edge;
		isUsed[next_node]=1;
		min_edge = 3000;
		count += 1;
	}
	printf("%d\n", sum);
	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=INT_MAX;
		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;
}

C++: #include <iostream>
#define N 100
#define INFINITY 2000000000
using namespace std;
void prim();
int M[N][N],d[N],p[N],u,n,mincost,i,j;
string color[N];
int main(){
  cin >> n;
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      cin >> M[i][j];
    }
  }
  prim();
  return 0;
}
void prim(){
  for(i=0;i<n;i++) color[i]="WHITE",d[i]=INFINITY;
  d[0]=0;
  p[0]=-1;
  while(1){
    mincost=INFINITY;
    for(i=0;i<n;i++){
      if(color[i]!="BLACK"&&d[i]<mincost){
	mincost=d[i];
	u=i;
      }
    }
    if(mincost==INFINITY) break;
    color[u]="BLACK";
    for(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]="GLAY";
	}
      }
    }
  }
  int sum=0;
  for(i=0;i<n;i++) sum+=d[i];
  cout << sum << endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#define INF 1e9
using namespace std;

int main(){
    //declaration and input
    int n;
    cin >> n;
    vector<vector<int>> graph(n, vector<int>(n));
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> graph[i][j];
    
    //get Minimum Spanning Tree by Prim's algorithm
    vector<int> parent_of(n, -1);
    vector<int> cost(n, INF);
    vector<int> state(n, 0);
    cost[0] = 0;
    state[0]++;

    int sum = 0;
    int parent = -1;
    while (true){
        int min_cost = INF;
        int next = -1;
        for (int i = 0; i < n; i++){
            if (state[i] == 1 && cost[i] < min_cost){
                min_cost = cost[i];
                next = i;
            }
        }
        if (min_cost == INF)
            break;
        sum += cost[next];
        state[next] = 2;
        for (int i = 0; i < n; i++){
            if (state[i] != 2 && graph[i][next] != -1){
                state[i] = 1;
                if (cost[i] > graph[i][next])
                    cost[i] = graph[i][next];
            }
        }
    }

    /*int sum = 0;
    for (int i = 0; i < n; i++){
        if (parent_of[i] != -1)
            sum += graph[i][parent_of[i]];
    }*/

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

C++: #include<iostream>
#include<unordered_set>
#define N 105
#define UNDEFINED -1
static const int INF = (1<<21);

int n,a;
int d[N], p[N];
int G[N][N];

int prim() {
    int u, min;
    std::unordered_set<int> hasVisited;
    for(int i = 0; i < N; i++) {
        d[i] = INF;
        p[i] = UNDEFINED;
    }
    d[0] = 0;
    while(true) {
        min = INF;
        u = UNDEFINED;
        for(int i = 0; i < n; i++) {
            if(d[i] < min && hasVisited.find(i) == hasVisited.end()) {
                u = i;
                min = d[i];
            }
        }
        if (u == UNDEFINED) break;
        hasVisited.insert(u);
        for(int i = 0; i < n; i++) {
            if(hasVisited.find(i) == hasVisited.end() && G[u][i] != UNDEFINED) {
                if(d[i] > G[u][i]) {
                    d[i] = G[u][i];
                    p[i] = u;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0; i < n; i++) {
        if(p[i] != UNDEFINED) {
            sum += G[i][p[i]];
        }
    }
    return sum;
}

int main() {
    std::cin >> n;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {

            std::cin >> a;
            G[i][j] = a;
        }
    }
    std::cout << prim() << std::endl;
    return 0;
}