Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] > 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A > edges[j].A })

	sum := 0
	for len(edges) > 0 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

C++: #include<iostream>
#include<vector>
#include<queue>

using namespace std;
using Graph=vector<vector<int>>;



struct Edge {
	int node1, node2, weight;

	Edge() {}

	Edge(int n1, int n2, int w) :node1(n1), node2(n2), weight(w) {}

	void numset(int n1, int n2, int w) {
		node1 = n1; node2 = n2; weight = w;
	}

};

void eswap(Edge& e1, Edge& e2) {
	Edge temp = e1;   e1 = e2;  e2 = temp;
}

void bubbleSort(vector<Edge>& edge) {
	bool flag;
	for (int i = 0; i < edge.size(); i++) {
		flag = true;
		for (int j = 0; j < edge.size()-i-1; j++) {
			if (edge.at(j).weight > edge.at(j + 1).weight) {
				eswap(edge.at(j), edge.at(j + 1));
				flag = false;
			}
		}
		if (flag) break;
	}
}

int kruskal(Graph &spntree,vector<Edge>& edgeset) {
	int size = spntree.size();
	queue<int> unconnected;
	vector<bool> connected(size,false);
	
	if (edgeset.size() == 0) return 0;
	bubbleSort(edgeset);

	connected.at(edgeset.at(0).node1)=true;
	connected.at(edgeset.at(0).node2) = true;
	spntree.at(edgeset.at(0).node1).push_back(edgeset.at(0).node2);
	spntree.at(edgeset.at(0).node2).push_back(edgeset.at(0).node1);
	int index=0,count = 2,cost=edgeset.at(0).weight;
	Edge ed;
	edgeset.erase(edgeset.begin());

	while (count<size) {
		ed = edgeset.at(index);
		if (connected.at(ed.node1) == true && connected.at(ed.node2) == false) {
			connected.at(ed.node2) = true;
			spntree.at(ed.node1).push_back(ed.node2);
			spntree.at(ed.node2).push_back(ed.node1);
			edgeset.erase(edgeset.begin() + index);
			count++; cost += ed.weight;
			index = 0; continue;
		}
		else if (connected.at(ed.node2) == true && connected.at(ed.node1) == false) {
			connected.at(ed.node1) = true;
			spntree.at(ed.node1).push_back(ed.node2);
			spntree.at(ed.node2).push_back(ed.node1);
			edgeset.erase(edgeset.begin() + index);
			count++; cost += ed.weight;
			index = 0; continue;
		}
		index++;
	}
	return cost;


}

int main(void) {

	int size, temp,cost,edgenum=0;
	Edge etemp;
	vector<Edge> edgeset;
	cin >> size;
	Graph spntree(size);
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size; j++) {
			cin >> temp;
			if (i < j) {
				if (temp != -1) {
					edgenum++;
					etemp.numset(i, j, temp);
					edgeset.push_back(etemp);
				}
			}
		}
	}

	cost=kruskal(spntree, edgeset);
	cout << cost << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxx = 1010;
const int INF = 1 << 30;
int n, m[maxx][maxx];
int u, num, v;
int flag[maxx];

int prim() {
	// d??°????????????V-T??? p??°????????°???MST???
	int d[maxx], p[maxx];
	for (int i = 0; i < n; i++) {
		d[i] = INF;
		p[i] = -1;
		flag[i] = 0;
	}

	d[0] = 0;
	while (1) {
		int minv = INF;
		u = -1;
		// ???????????????????°???????
		for (int i = 0; i < n; i++) {
			if (minv > d[i] && flag[i] == 0) {
				u = i;
				minv = d[i];
			}
		}

		if (u == -1)
			break;

		// ?????°???????????\MST
		flag[u] = 1;

		// ??´??°?????????????????????????????????????????´???????°???????????????????????????????
		for (int v = 0; v < n; v++) {
			if (flag[v] == 0 && m[u][v] != INF && d[v] > m[u][v]) {
				d[v] = m[u][v];
				p[v] = u;
			}
		}
	}


	int ans = 0;
	for (int i = 0; i < n; i++)
		if (p[i] != -1)
			ans += m[i][p[i]];

	return ans;
}
int main()
{
	memset(flag, 0, sizeof(flag));
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			scanf("%d", &m[i][j]);
			if (m[i][j] == -1)
				m[i][j] = INF;
		}
	}
	printf("%d\n", prim());
	return 0;
}

C++: #include <iostream>
#include <array>
#include <vector>
#include <algorithm>

int n;
static const int nmax = 2005;
static const int INFTY = (1 << 21);
static const int WHITE = 0; // Not visited
static const int GRAY = 1;  // Visited
static const int BLACK = 2; // In MST
std::array<std::array<int, nmax>, nmax> adjmat;
std::array<int, nmax> d, p;
std::array<int, nmax> color;

void prim()
{
  color.fill(WHITE);
  d.fill(INFTY);
  p.fill(-1);

  int v = 0;
  d[v] = 0;
  color[v] = GRAY;

  while (true)
    {
      auto mincost = INFTY;

      // Find minimum cost vertex from current MST
      for (int i=0; i<n; ++i)
        {
          if (color[i] != BLACK && d[i] < mincost)
            {
              mincost = d[i];
              v = i;
            }
        }

      // Break if no update
      if (mincost == INFTY) break;

      // Include v in MST
      color[v] = BLACK;

      // Update minimum cost outside current MST
      for (int i=0; i<n; ++i)
        {
          if (color[i] != BLACK && adjmat[v][i] != -1 && adjmat[v][i] < d[i])
            {
              d[i] = adjmat[v][i];
              color[i] = GRAY;
              p[i] = v;
            }
        }
    }
}

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

  prim();

  int ans = 0;
  //std::for_each(d.begin(), d.begin()+n, [&](auto x){ans+=x;});
  for (int i=0; i<n; ++i)
    {
      if (p[i] != -1)
        ans += adjmat[i][p[i]];
    }
  std::cout << ans << std::endl;
}

C++: //
//  main.cpp
//  minnimum_spanning_tree
//
//  Created by ???????????? on 2017/10/23.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>

#define n_max 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 3000

using namespace std;

int n, M[n_max][n_max], color[n_max], d[n_max], p[n_max];

void init(){
    int i, j;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            scanf("%d", &M[i][j]);
        }
        color[i] = WHITE;
        d[i] = INFTY;
    }
}

void prim(){
    //initializing
    d[0] = 0;
    p[0] = -1;
    
    int u;
    while(true){
        int mincost = INFTY;
        for(int i = 0; i < n; i++ ){
            if((color[i] != BLACK) && (d[i] < mincost)){
                mincost = d[i];
                u = i;
            }
        }
        
        if (mincost == INFTY) break;
        
        color[u] = BLACK;
        
        for(int v = 0; v < n; v++){
            if((color[v] != BLACK) && (M[u][v] != -1)){
                if(M[u][v] < d[v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0; i < n; i++){
        sum += d[i];
    }
    cout<<sum<<endl;
}

int main(int argc, const char * argv[]) {
    init();
    prim();
//    std::cout << "Hello, World!\n";
    return 0;
}