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 <bits/stdc++.h>
#define rep(i,a) for(i=0;i<a;i++)
using namespace std;
static const int W = 0;
static const int G = 1;
static const int B = 2;

void prim(int n, int &total,vector<vector<int> > &M) {
    int i,u,min;
    vector<int> d(n);
    vector<int> p(n);
    vector<int> color(n);

    rep(i,n){
        d[i] = INT_MAX;
        p[i] = -1;
        color[i] = W;
    }
    d[0] = 0;
    while(true){
        min = INT_MAX;
        u = -1;
        rep(i,n){
            if(min > d[i] && color[i] != B) {
                u = i;
                min = d[i];
            }
        }
        if(u==-1) break;
        color[u] = B;
        rep(i,n){
            if(color[i] != B && M[u][i] != INT_MAX){
                if(d[i] > M[u][i]) {
                    d[i] = M[u][i];
                    p[i] = u;
                    color[i] = G;
                }
            }
        }
    }
    rep(i,n){
        if(p[i] != -1) total += M[i][p[i]];
    }
}

int main(void) {
    int i,j,n,total = 0;
    vector<vector<int> > M;
    cin >> n;
    M.resize(n);

    rep(i,n){
        M[i].resize(n);
    }
    rep(i,n){
        rep(j,n){
            cin >> M[i][j];
            if(M[i][j] == -1) M[i][j] = INT_MAX;
        }
    }
    prim(n,total,M);
    cout << total << 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; // u??????
    int d[MAX], p[MAX], color[MAX]; // d:??¢?´¢??????????±?, p:MST??????????????????v??????

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

    d[0] = 0; // MST?????¢?´¢????§?????????????????????°???

    while (1){
	    minv = INFTY;
	    u = -1;
	    for(int i = 0; i < n; i++){
		    // ??¢?´¢????±?????????§??????????°??????????????????§??????????????¢?´¢???????????????????????????	
		    if(minv > d[i] && color[i] != BLACK){
			    u = i; // MST?????????????????????????±???????
			    minv = d[i];
		    }
	    }
	    if(u == -1) break; //??¨??????????±??????¢?´¢????????¨???????????¢?´¢??????????????? 
	    color[u] = BLACK; // ??¢?´¢???????????????
	    for(int v = 0; v < n; v++){
		   //?????¢?´¢????????§?????????u??¨?????????????????£???????????????
		   //  ?¬??????¢?´¢????±???¨????????????????????????????????????
		   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]]; // MST???????????????????????¨???
    }

    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;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int BLACK=2;
static const int GRAY=1;
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(void){
    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>
#include <algorithm>
#include <climits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

int n;
vector<int> g[101];
vector<int> d(101);
vector<int> p(101);
vector<int> vis(101);

void prim(){
    for (int i = 0; i < n; ++i) {
        vis[i] = 0;
        d[i] = INT_MAX;
        p[i] = -1;
    }

    d[0] = 0;
    while(true) {
        int mincost = INT_MAX;
        int next = -1;
        for (int i = 0; i < n; ++i) {
            if(vis[i] == 0 && d[i] < mincost) {
                mincost = d[i];
                next = i;
            }
        }
        if(next == -1) break;
        vis[next] = 1;

        for (int j = 0; j < n; ++j) {
            if(g[next][j] < d[j] && vis[j] != 1 && g[next][j] != -1) {
                p[j] = next;
                d[j] = g[next][j];
            }
        }
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int v;
            cin >> v;
            g[i].push_back(v);
        }
    }

    prim();

    long cst=0;
    for (int k = 0; k < n; ++k) {
        cst += d[k];
    }
    cout << cst << endl;

    return 0;
}