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 <stdio.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;

class edge
{
  public:
    int i, j, weigt;
    bool operator()(edge e1, edge e2)
    {
        return e1.weigt < e2.weigt;
    }
};
edge e[10000];
int main()
{
    int n;
    cin >> n;
    int b[n][n] = {};
    int k = 0;
    int tmp = 0;
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> tmp;
            if (j < i + 1 || tmp < 0)
                continue;
            e[k].i = i;
            e[k].j = j;
            e[k].weigt = tmp;
            k++;
        }
    }
    sort(e, e + k, edge());
    for (int i = 0; i < k; i++)
    {
        if (b[e[i].i][e[i].j] == 0)
        {
            sum += e[i].weigt;
            b[e[i].i][e[i].j] = 1;
            b[e[i].j][e[i].i] = 1;
            for (int j = 0; j < n; j++)
            {
                if (b[e[i].i][j] || b[e[i].j][j])
                    b[e[i].i][j] = b[e[i].j][j] = b[j][e[i].i] = b[j][e[i].j] = 1;
            }
        }
    }
    cout << sum << "\n";
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <climits>
#include <list>
#include <queue>

using namespace std;

static const int INFTY = 2001;

int main() {
	int Dist[101];
	int AdjacenceyList[100][100];
	int AdjacenceyWeightList[100][100];
	int Degree[100];
	int i, j;
	int N;
	int W;
	int Deg;
	int MinTotalWeight = 0;
	int u, v;
	int PrevSecondSmallestVertex;
	int SecondSmallestVertex;
	int MinVertex;
	bool inT[100];

	cin >> N;

	for (i = 0; i < N; i++) {
		inT[i] = false;
		Dist[i] = INFTY;
		Deg = -1;
		for (j = 0; j < N; j++) {
			cin >> W;
			if(W != -1){
				AdjacenceyList[i][++Deg] = j;
				AdjacenceyWeightList[i][Deg] = W;
			}
		}
		Degree[i] = Deg + 1;
	}
	Dist[100] = INFTY;

	u = 0;
	inT[u] = true;
	Dist[u] = 0;
	PrevSecondSmallestVertex = 100;
	for (i = 1; i <= N - 1; i++) {
		MinVertex = 100;
		for (j = 0; j < Degree[u]; j++) {
			v = AdjacenceyList[u][j];
			if (inT[v] == false) {
				W = AdjacenceyWeightList[u][j];
				if (W < Dist[v]) {
					Dist[v] = W;
				}
				if (Dist[v] < Dist[MinVertex]) {
					SecondSmallestVertex = MinVertex;
					MinVertex = v;
				}
				else if (Dist[v] < Dist[SecondSmallestVertex]) {
					SecondSmallestVertex = v;
				}
			}
		}

		//DEBUG
		cout << "MinVerTex = " << MinVertex << " SecondSmallestVertex = " << SecondSmallestVertex << endl;

		if (Dist[PrevSecondSmallestVertex] < Dist[MinVertex]) {
			SecondSmallestVertex = MinVertex;
			MinVertex = PrevSecondSmallestVertex;
			PrevSecondSmallestVertex = SecondSmallestVertex;
		}
		else if (Dist[SecondSmallestVertex] < Dist[PrevSecondSmallestVertex]) {
			PrevSecondSmallestVertex = SecondSmallestVertex;
		}
		inT[MinVertex] = true;
		u = MinVertex;
		MinTotalWeight += Dist[MinVertex];

		//DEBUG
		cout << "MinVertex = " << MinVertex << " PrevSecondSmallestVertex = " << PrevSecondSmallestVertex << endl;
		cout << "inT: ";
		for (int k = 0; k < N; k++) {
			cout << inT[k] << " ";
		}
		cout << endl;
		printf("MinTotalWeight = %d\n", MinTotalWeight);
	}
	//printf("%d\n", MinTotalWeight);

	return 0;
}

C++: #include<iostream>
using namespace std;
#define INF  (1<<21)
//int prim(int M[100][100], int n){
int M[100][100];
int prim(int n){
	int u, mincost;
	int color[n], d[n], p[n];
	for(int i=0; i<n; i++){
		color[i]=0;
		d[i]=INF;
		p[i]=-1;
	}
	d[0]=0;
	while(1){
		mincost=INF;
		u=-1;
		for(int i=0; i<n; i++){
			if(mincost>d[i] && color[i]!=2){
				u=i;
				mincost=d[i];
			}
		}
		if(u==-1)
			break;
		color[u]=2;
		for(int v=0; v<n; v++){
			if(color[v]!=2 && M[u][v]!=INF){
				if(d[v]>M[u][v]){
					d[v]=M[u][v];
					p[v]=u;
					color[v]=1;
				}
			}
		}
	}
	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;
	//int M[100][100];
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			int e;
			cin>>e;
			if(e!=-1)
				M[i][j]=e;
			else
				M[i][j]=INF;
		}
	}
	cout<<prim(n)<<endl;
	return 0;
}

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

typedef pair<int, int> P;
const int M = 1000000007;

int prim_cost(vector<vector<P>> edge) {
    int n = edge.size();
    vector<int> vis(n, 0);
    int c = 0;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(P(0, 0));
    while (!q.empty()) {
        P p = q.top();
        q.pop();
        if (vis[p.second]) continue;
        vis[p.second] = 1;
        c += p.first;
        for (auto& e : edge[p.second]) {
            if (!vis[e.second]) {
                q.push(P(e.first, e.second));
            }
        }
    }
    return c;
}


int main() {
    int n;
    cin >> n;
    vector<vector<P>> edge(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int a;
            cin >> a;
            if (a != -1)
                edge[i].push_back(P(a, j));
        }
    }
    cout << prim_cost(edge) << "\n";
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#define wh 0
#define gr 1
#define bl 2
#define inf 2001
#define null (-1)
#define Max 100 
using namespace std;
int color[Max],m[Max][Max];
int d[Max],p[Max],n;
void prim(){
int i,j,u;
	for(i=0;i<n;i++){
	color[i]=wh;
		d[i]=inf;
	}
	d[0]=0;
	p[0]=-1;
	while(true){
	int mincost=inf;
		for(i=0;i<n;i++){
			if(color[i]!=bl && d[i]<mincost){
			mincost=d[i];
				u=i;
			}
		}
		if(mincost==inf)
		break;
		color[u]=bl;
		for(int v=0;v<n;v++){
			if(color[v]!=bl && m[u][v]!=null){
				if(m[u][v]<d[v]){
				d[v]=m[u][v];
					p[v]=u;
					color[v]=gr;
				}
			}
		}
	}
}

main(){
	int i,j;
	cin>>n;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
	cin>>m[i][j];
			m[j][i]=m[i][j];
		}
	}
	prim();
	int ans=0;
	for(i=0;i<n;i++)
	ans+=d[i];
	cout<<ans<<endl;
	return 0;
}