Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			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] != INFTY {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

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

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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

using namespace std;

#define MAX_V 100
#define MAX_E 5050

int par[MAX_V],rank[MAX_V];
int V,E;

struct edge{ int u,v,cost; };
edge es[MAX_E];

bool comp(const edge &e1, const edge &e2){
  return e1.cost < e2.cost;
}

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);
}

int kruskal(){
  sort(es, es + E, comp);

  init(V);
  int res = 0;

  for(int i = 0 ; i < E ; i++){
    edge e = es[i];

    if(!same(e.u,e.v)){
      unite(e.u,e.v);
      res += e.cost;
    }
  }
  return res;
}

int main(){
  int c;

  cin >> V;
  for(int i = 0 ; i < V ; i++){
    for(int j = 0 ; j < V ; j++){
      cin >> c;

      if(c == -1){
	continue;
      }

      edge e = {i,j,c};
      es[E++] = e;
    }
  }

  cout << kruskal() << endl; 

  return 0;
}

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

const int INF = (1<<30)-1;

template<typename T>
T gcd(T a, T b)
{
	return b ? gcd(b, a%b) : a;
}

template<typename T>
T lcm(T a, T b)
{
	return a / gcd(a, b) * b;
}

//(a^e)%mod
template<typename T>
T power(T a, T e, T mod)
{
	if(e == 0)
	{
		return 1;
	}
	if(e % 2 != 0)
	{
		//a^e=a^(e-1)*a
		return (power(a, e - 1, mod) * a) % mod;
	}
	T res = power(a, e / 2, mod);
	//a^e=a^(e/2)*a^(e/2)
	return (res * res) % mod;
}

template<typename T>
class Tree
{
private:
public:
	T data;
	Tree* parent = NULL;
	std::vector<Tree*> children;
	void setData(T x)
	{
		data = x;
	}
	void setParent(Tree* t)
	{
		parent = t;
	}
	void addChild(Tree* t)
	{
		children.push_back(t);
	}
	Tree(){}
	~Tree(){}
};

template<typename T>
class Graph
{
public:
	T index;
	vector<T> edge;
	void setEdge(T t)
	{
		edge.push_back(t);
	}
	Graph(){}
	~Graph(){}
};

int prim(int size, int cost[101][101])
{
	const int indexed = 1;
	const int WHITE = 0;
	const int BLACK = 1;
	const int GRAY = 2;
	int colour[size + indexed], d[size + indexed], p[size + indexed];
	int u;
	for (int i = indexed; i < size + indexed; ++i)
	{
		colour[i] = WHITE;
		d[i] = INF;
	}
	d[indexed] = 0;
	p[indexed] = -1;

	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> PQ;
	PQ.push(make_pair(0, indexed));

	while(!PQ.empty())
	{
		u = PQ.top().second;
		PQ.pop();
		colour[u] = BLACK;
		for (int i = indexed; i < size + indexed; ++i)
		{
			if(colour[i] != BLACK && cost[u][i] != INF)
			{
				if(cost[u][i] < d[i])
				{
					PQ.push(make_pair(cost[u][i], i));
					d[i] = cost[u][i];
					p[i] = u;
					colour[i] = GRAY;
				}
			}
		}
	}
	int sum = 0;
	for (int i = indexed; i < size + indexed; ++i)
	{
		if(p[i] != -1)
		{
			sum += cost[i][p[i]];
		}
		// cout << i << " " << p[i] << endl;
	}
	return sum;
}

int main()
{
	using namespace std;
	Graph<int> G[101];
	int costn[101][101];
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			int e;
			cin >> e;
			costn[i][j] = (e == -1 ? INF : e);
		}
	}
	cout << prim(n, costn) << endl;

	return 0;
}

C++: #if 0
????????????????§£????????????????????????????????????????????????AOJ????????????????????????????????????wrong???answer?????????????????????
????????§?????????????????\????????????????????£??????????????§??????????????????????????????????????¨???????????????
???????????????????????? Union Find Tree ???????????????????????????????????????????????????????????????????????????Root?????¢?????°??????????????¨??§??????
??????????????§???????????????????????????????????????????????¢??°???????????¢??°?????????????????£??????????????°???????????°???????\???????????????¨???????????¨???
????????????????????????
Minimum Spanning Tree ???????????§???????????¶??????????????????????????¨????????????????????¨?????????????????????????????????????????????????????£?????????
???????????????????????????????????????
#endif

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


struct edge
{
   int cost;
   int src;
   int dst;
};

edge e[5001];
int A[101][101];
int M=0;
int P[101];

void init(int N)
{
  for (int i=0; i<=N; ++i) P[i] = i;
}

int root(int a)
{
  if (P[a] == a) return a;
  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);
}

void checkEdges(int n)
{
    for(int r=0;r<n;r++)
    {
        for(int c=r+1;c<n;c++)
            {
                if(A[r][c]>-1)
                    {
                        e[M].cost=A[r][c];
                        e[M].src=r;
                        e[M].dst=c;
                        M+=1;
                    }
            }
    }
}

bool cmp(edge e1,edge e2)
{
    return e1.cost<=e2.cost;
}
int main()
{
    int n,count=0;
    cin>>n;
    init(n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++){
                cin>>A[i][j];
        }
    }
    checkEdges(n);
    sort(e,e+M,cmp);
    for (int i=0; i<M; ++i)
    {
        if(is_same_set(e[i].src,e[i].dst))
        {
            continue;
        }
        unite(e[i].dst,e[i].src);
        count+=e[i].cost;
    }
    cout<<count<<endl;
    return 0;
}