Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include<cstdio>
#include <vector>
#include<queue>
#include<limits.h>
#include<string.h>
#include<functional>
#include<map>
#include<set>
#include<list>
#include<unordered_map>
#include<queue>
#define P pair<int,int>
using namespace std;

int a[100][100];
bool used[100];
signed main(){
	int b; cin >> b;
	for (int c = 0; c < b; c++) {
		for (int d = 0; d < b; d++) {
			cin >> a[c][d];
			if (a[c][d] == -1) {
				a[c][d] = INT_MAX / 3;
			}
		}
	}
	int ans = 0;
	priority_queue<P,vector<P>, greater<P>>Q;
	Q.push({ 0,0 });
	while (Q.size()) {
		P t = Q.top(); Q.pop();
		if (!used[t.second]) {
			ans += t.first;
			used[t.second] = true;
			for (int i = 0; i < b; i++) {
				Q.push({ a[t.second][i],i });
			}
		}
	}
	cout << ans << endl;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <cfloat>
#include <math.h>
#include <numeric>
#include <string.h>
#include <sys/time.h>
#include <random>


#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;


class Graph{
    struct edge{
        int vertice;
        ll weight;
    };

    vector<vector<edge>> g;
    unsigned int n;

public:
    Graph(unsigned int n): n{n}{
        g.resize(n);
    }

    void add_edge(int u, int v){
        g[u].emplace_back(edge{v, 1});
    };

    void add_edge(int u, int v, ll w){
        g[u].emplace_back(edge{v, w});
    }

    vector<edge>& get_edge(int v){
        return g[v];
    }

    unsigned int get_num_vertices() const{
        return n;
    }

    unsigned int size() const{
        return n;
    }

    vector<edge>& operator[](int v){
        return get_edge(v);
    }
};


/**
 * prim法 (O(|E|log(|V|))のアルゴリズム)
 * @param g 連結グラフ
 * @return 連結グラフの最小域木をグラフとして返す
 */
Graph prim2_g(Graph g){
    // Ptmp.fsが今の部分木からのコスト、Ptmp.ｓｃ.fsが次の頂点、Ptmp.sc.scが次の頂点に辺を伸ばす部分木の頂点
    typedef pair<ll, pair<int, int>> Ptmp;

    unsigned int n = g.size();

    Graph mst(n);   // minimum spanning tree
    vector<ll> used(n, false);
    priority_queue<Ptmp, vector<Ptmp>, greater<Ptmp> > que;

    used[0] = true;
    for(auto e: g[0]){
        que.push(Ptmp(e.weight, pair<int, int>(e.vertice, 0)));
    }

    while(!que.empty()){
        Ptmp p = que.top(); que.pop();
        int to = p.second.first, from = p.second.second;
        if(used[to]) continue;

        used[to] = true;
        mst.add_edge(from, to, p.first);
        mst.add_edge(to, from, p.first);

        for(auto e: g[to]){
            if(!used[e.vertice]){
                que.push(Ptmp(e.weight, pair<int, int>(e.vertice, to)));
            }
        }
    }

    return mst;
}


ll prim2(Graph g){
    unsigned int n = g.size();
    Graph mst = prim2_g(g);

    ll res = 0;
    for(int i = 0; i < n; i++){
        for(auto e: mst[i]){
            res += e.weight;
        }
    }

    return res / 2;
}



int main(){
    int n;   cin >> n;
    Graph g(n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int x;  cin >> x;
            if(x != -1){
                g.add_edge(i, j, x);
            }
        }
    }

    cout << prim2(g) << endl;

    return 0;
}

C++: #include <iostream>

using namespace std;
static const int MAX=100;
static const int INF=1<<21;

int p[MAX]; //????°???????????????????????????¶??????
int d[MAX]; //??????????¨??????????????????????????????????????????????
int M[MAX][MAX]; //u???v????????????
bool visited[MAX]; //??????????¢??????\

int prim(int n)
{
    //init
    for(int i=0;i<n;i++)
    {
        p[i]=-1;
        visited[i]=false;
        d[i]=INF; //????§???????????????????d?????????????????§
    }

    d[0]=0; //?¬¬0?????????d?????´??°
    while(true)
    {
        //??????d???????°????????????\??????
        int min=INF;
        int min_point=-1;
        for(int i=0;i<n;i++)
        {
            if(d[i]<min&&visited[i]==false)
            {
                min_point=i;
                min=d[i];
            }
        }
        if(min==INF) break;
        visited[min_point]=true;
        //??´??°???????????°?????\????????????????????????d???
        for(int i=0;i<n;i++)
        {
            if(M[min_point][i]!=INF&&visited[i]==false)
                if(d[i]>M[min_point][i])
                {
                    d[i] = M[min_point][i]; //??????d????????§????°±????????´??°
                    p[i]=min_point; //??????
                }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++)
        if(p[i]!=-1) //??????????°?????????????????????¶
           sum+=M[i][p[i]];
    return sum;
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>M[i][j];
            M[i][j]=(M[i][j]==-1?INF:M[i][j]);
        }
    }
    cout<<prim(n)<<endl;
}

C++: #include <iostream>

using namespace std;
int n;
int dat[100][100];
int cost[100];
int done[100];

void prim(){
	for(int i=0;i<n;i++){
		cost[i]=1<<21;
		done[i]=0;
	}
	int s=0;	
	int next, ncost=1<<21;
	done[s]=1;
	cost[s]=0;
	while(1){
		ncost=1<<21;
		for(int j=0;j<n;j++){
			if(done[j]){
				for(int i=0;i<n;i++){
					if(dat[j][i]!=-1){
						if(cost[i]>=dat[j][i]&&!done[i]){
							cost[i]=dat[j][i];
						}
						if(ncost>cost[i]&&!done[i]){
							next=i;
							ncost=cost[i];
						}
					}
				}
			}
		}
//		cout<<"next:"<<next<<" cost:"<<cost[next]<<endl;
		done[next]=1;
		int flag=true;
		for(int i=0;i<n;i++){
			if(!done[i]) flag = false;
		}
		if(flag)
			return;
	}
}


int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>dat[i][j];
		}
	}
	prim();
	int sum=0;
	for(int i=0;i<n;i++){
		sum+=cost[i];
	}
	cout<<sum<<endl;
	return 0;
}