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 <limits.h>
using namespace std;

int main(void)
{
	int n, now = 0, flag[101] = {};
	long ans = 0, an[100][100], min[100];

	cin >> n;

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

	do {
		long key = LONG_MAX, key_i;
		flag[now] = 1;
		flag[100] = 0;

		for(int i = 0; i < n; i++) {
			if(an[now][i] != -1 && an[now][i] < min[i] && flag[i] == 0)
				min[i] = an[now][i];
			if(flag[i] == 0 && min[i] < key) {
				key = min[i];
				key_i = i;
			}
			if(flag[i] == 1)
				flag[100]++;
		}

		now = key_i;
		ans += key;

	}while(flag[100] != n - 1);

	cout << ans << endl;

	return 0;
}

C++: #if 0
AOJ??????accepted??????????????????
???????????????60??????????????§?????????

pair???????????????????????¨
????????¨?????????pair?????????????????????????????¨?????????????????????????????????
#endif

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

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

int root(int a){//a???root????±???????
  if(P[a]==a)
    return a;
  return (P[a]=root(P[a]));
}

bool is_same_set(int a,int b){//??????set?????????
  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 num=N*N;
  int cost[num];
  int src[num],dst[num];
  for (int r=0; r<N;r++){
    for (int c=0; c<r;c++){
      if (A[r][c] > -1) {
	cost[M] = A[r][c];
	src[M] = r;
	dst[M] = c;
	M += 1;
      }
    }
  }

  pair<int,int> edge[100000+10];//?§??????????????????¢????????????
  for(int i=0; i<M;i++){
    edge[i]=make_pair(cost[i],i);
  }
  sort(edge,edge+M);

  int edges[M][3];
  for(int i=0; i<M;i++){//?????????????????????
    edges[i][0]=edge[i].first;
    edges[i][1]=src[edge[i].second];
    edges[i][2]=dst[edge[i].second];
  }
  /*
  check!
  for(int i=0; i<M;i++){
    cout<<"news cost="<<edges[i][0]<<" ,"<<edges[i][1]<<"-"<<edges[i][2]<<endl;
  }
  */




  init(N);
  int sum=0;
  for(int i=0; i<M;i++){//???????????????????????????
    if(is_same_set(edges[i][1],edges[i][2])==0){
      unite(edges[i][1],edges[i][2]);
      sum=sum+edges[i][0];
    }
  }
    cout<<sum<<endl;
}

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

class union_find  // union find.
{
    int* U;
public:
    union_find(int n){ U = new int [n + 1]; for(int i = 1; i <= n; i++) U[i] = i; }
    int root(int a){ if(U[a] == a){ return a; } U[a] = root(U[a]); return U[a]; }
    bool same(int a, int b){ return root(a) == root(b); }
    void join(int a, int b){ int x = root(a), y = root(b); U[x] = y; }
    ~union_find(){ delete [] U; }
};

struct edge{ int v1, v2; int weight; };  // edge.

edge init(){
    edge e;
    e.v1 = 0; e.v2 = 0; e.weight = -1;
    return e;
}

edge init(int a, int b, int w){
    edge e;
    e.v1 = a; e.v2 = b; e.weight = w;
    return e;
}

void swap_e(edge e1, edge e2)
{
    swap(e1.v1, e2.v1); swap(e1.v2, e2.v2); swap(e1.weight, e2.weight);
}

class edge_heap  // heap.
{
    edge* E;
    int edge_n;
public:
    edge_heap(int n){ E = new edge [n + 1]; edge_n = 0; }
    void min_heap(int i);
    void insert_heap(edge e);   // sorting.
    void insert_nl(edge e);     // no sorting.
    edge delete_min();
    bool isEmpty(){ return edge_n == 0; }
    int total_weight();
    ~edge_heap(){ delete [] E; }
};

void edge_heap::min_heap(int i)
{
    int l = 2 * i, r = 2 * i + 1;
    if(l > edge_n && r > edge_n) return;
    int minid = i;
    if(l <= edge_n){ if(E[l].weight < E[minid].weight) minid = l; }
    if(r <= edge_n){ if(E[r].weight < E[minid].weight) minid = r; }
    if(minid == i) return;
    swap(E[minid], E[i]);
    min_heap(minid);
}

void edge_heap::insert_heap(edge e)
{
    edge_n++;
    int w = e.weight;
    int i = edge_n;
    while(i > 1){
        if(w < E[i / 2].weight){
        E[i] = E[i / 2];
        }else{ break; }
        i /= 2;
    };
    E[i] = e;
}
void edge_heap::insert_nl(edge e){ edge_n++; E[edge_n] = e; }

edge edge_heap::delete_min()
{
    edge e = E[1];
    E[1] = E[edge_n]; edge_n--;
    min_heap(1);
    return e;
}

int edge_heap::total_weight()
{
    int tw = 0;
    for(int i = 1; i <= edge_n; i++) tw += E[i].weight;
    return tw;
}

int main()
{
    int n, i, j, w;
    scanf("%d", &n);
    edge_heap EH(n * (n + 1) / 2); // All Edge heap.
    edge e;

    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++){
            scanf("%d", &w);
            if(j <= i || w < 0) continue;
            e = init(i, j, w);
            EH.insert_heap(e);
        }
    }

    edge_heap TH(n);   // Tree heap.

    union_find VG(n);  // vertex group.

    while(!EH.isEmpty()){
        e = EH.delete_min();
        if(!VG.same(e.v1, e.v2)){
            VG.join(e.v1, e.v2);
            TH.insert_nl(e);
        };
    };

    printf("%d", TH.total_weight());

    return 0;
}