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
	num := 0
	for len(edges) > 0 && num < n-1 {
		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
		num += 1
	}
	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<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

#include<queue>

using namespace std;

#define MAX_N 100
// 値の定義
const int INV = -1;
const int MAX_W = 2000;

// グラフ情報を保持する配列
int ary_graph[MAX_N + 1][MAX_N + 1];

// 入力された頂点を保持するキュー
queue<int> Base_Queue;

int n = 0;

// プリム法で最小全域木を求める
int MinimumSpanningTree(int start_vertex);

int main(void)
{
    int sum = 0;

    for(int i = 0; i < MAX_N + 1; i++)
    {
        for(int j = 0; j < MAX_N + 1; j++)
        {
            // 配列の初期化
            ary_graph[i][j] = INV;
        }
    }

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        // グラフを構成する頂点の格納
        Base_Queue.push(i);

        for(int j = 0; j < n; j++)
        {
            // 隣接行列の設定
            cin >> ary_graph[i][j];
        }
    }

    // 頂点１から最小全域木を探索する
    sum = MinimumSpanningTree(0);

    // 結果の表示
    printf("%d\n", sum);

    return 0;
}

// 最小全域木を探索する関数
// start_vertex : 起点となる頂点のNo.
int MinimumSpanningTree(int start_vertex)
{

    int q_size_1, q_size_2, min_val, min_ele, s, t, tmp;
    // 辺の和を保持する変数
    int sum = 0;

    // 最小全域木を構成する頂点を保持するキュー
    queue<int> Prim_Queue;

    // 検索する頂点の初期値の設定
    s = Base_Queue.front();
    Base_Queue.pop();
    Prim_Queue.push(s);

    while( !Base_Queue.empty() )
    {
        min_val = MAX_W;
        min_ele = INV;
        q_size_1 = Base_Queue.size();
        q_size_2 = Prim_Queue.size();

        for( int i = q_size_2 - 1; i >= 0; i-- )
        {
            // s から t に向かう辺の重みを探索する
            s = Prim_Queue.front();
            Prim_Queue.pop();
            // 要素を順番に取り出すために, 取り出したものを元に戻す
            Prim_Queue.push(s);

            for( int i = q_size_1 - 1; i >= 0; i-- )
            {
                t = Base_Queue.front();
                Base_Queue.pop();

                if( ary_graph[s][t] != INV && min_val > ary_graph[s][t] )
                {
                    // 最小のコストを持つ, 辺を探索する
                    min_val = ary_graph[s][t];
                    min_ele = t;
                }

                // 一旦, 元に戻す
                Base_Queue.push(t);
            }
        }

        // 最小のコストを持つ辺の頂点を追加して, 木を構成する
        for(int i = 0; i < Base_Queue.size(); i++)
        {
            tmp = Base_Queue.front();
            Base_Queue.pop();

            if( tmp == min_ele )
            {
                Prim_Queue.push(tmp);
                break;
            }
            else
            {
                Base_Queue.push(tmp);
            }
        }

        sum += min_val;

    }

    return sum;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n,M[MAX][MAX];

int prim(){
  int u,minv;
  int d[MAX],p[MAX],color[MAX];

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

  d[0] = 0;

  while(1){
    minv = INFTY;
    u = -1;
    for(int 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(int 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;
	}
      }
    }
  }
  int sum = 0;
  for(int i = 0;i < n;i++){
    if(p[i] != -1) sum += M[i][p[i]];
  }

  return sum;
}

int main(){
  cin >> n;

  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      int e;cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

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

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

	cin >> n;

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

	/* ???????則?????????即?????属????????? */
	do {
		flag[now] = 1;
		flag[100] = 0;
/*
		cout << "\n??? " << now << " ????則???????????????????" << endl;
		cout << "?????贈?????? : ";
		for(int i = 0; i < n; i++) {
			if(flag[i] == 1) {
				cout << i << " ";
			}
		}
		cout << endl;
		cout << "?????即???????属?????????則" << endl;
		for(int i = 0; i < n; i++) {
			if(flag[i] == 0) {
				cout << "??? " << i << " : " << min[i] << endl;
			}
		}
*/
		/* min?????卒??属 */
		for(int j = 0; j < n; j++) {
			if(an[now][j] != -1 && an[now][j] < min[j] && flag[j] == 0) {
/*				cout << "??? " << j <<" ???????属?????????卒??属??????????????? : "<< min[j] << " ??? " << an[now][j] << endl; */
				min[j] = an[now][j];
			}
		}
/*
		cout << "?????即???????属?????????則" << endl;
		for(int i = 0; i < n; i++) {
			if(flag[i] == 0) {
				cout << "??? " << i << " : " << min[i] << endl;
			}
		}
*/
		/* ?測??????蔵?卒蔵???min????属???? */
		int key = 2001, key_j;

		for(int j = 0; j < n; j++) {
			if(flag[j] == 0 && min[j] < key) {
				key = min[j];
				key_j = j;
			}
		}
/*
		cout << "?則???????????賊??????????????????? : ??? " << key_j << endl;
*/
		/* ?則?????????? */
		now = key_j;
		ans += key;

		/* ??????????????贈????????? */
		for(int i = 0; i < n; i++) {
			if(flag[i] == 1) {
				flag[100]++;
			}
		}
	
	}while(flag[100] != n - 1);

	cout << ans << endl;

	return 0;
}

C++: #include <iostream>

using namespace std;

static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;


int n, M[MAX][MAX];

int prim()
{
    int u, minv;
    int d[MAX], p[MAX], color[MAX];
    int i;

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

    d[0] = 0;

    while (1) {
        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 (int 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;
                }
            }
        }
    }

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

    return sum;
}

int main()
{
    cin >> n;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int e; cin >> e;
            M[i][j] = (e == -1) ? INFTY : e;
        }
    }

    cout << prim() << endl;

    return 0;
}