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

static const int MAX =100;
static const int INFTY=numeric_limits<int>::max();

typedef enum {
    WHITE,
    GRAY,
    BLACK,
}Color;

int n,M[MAX][MAX];

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

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

    d[0]=0;

    while(true){
        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 k;
            cin>>k;
            M[i][j]=(k==-1) ? INFTY : k;
        }
    }

    cout<<Prim()<<endl;

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int n;
vector<vector<int>> a(100,vector<int>(100, -1));
vector<int> t, d(100, -1);

void prim(int start) {
    t.push_back(start);
    d[start] = 0;
    while (t.size() < n) {
        int min_v = 2001, min_i;
        rep(i, t.size()) {
            rep(j, n) {
                if (a[t[i]][j] != -1 && d[j] == -1 && a[t[i]][j] < min_v) {
                    min_v = a[t[i]][j];
                    min_i = j;
                }
            }
        }
        t.push_back(min_i);
        d[min_i] = min_v;
    }
    
}

int main() {
    scanf("%d", &n);
    rep(i, n) {
        rep(j, n) {
            scanf("%d", &a[i][j]);
        }
    }
    prim(0);
    int sum = 0;
    rep(i, n) {
        sum += d[i];
    }
    printf("%d\n", sum);
}

C++: #include<iostream>
#define INF 1<<30
using namespace std;
int main() {
  int n; cin >> n;
  int graph[101][101] = {};
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) cin >> graph[i][j];
  int mincost[101], flag[101] = {}, sum = 0;
  for (int i = 2; i <= n; ++i) mincost[i] = INF;
  mincost[1] = 0;
  while (1) {
    int v = -1;
    for (int i = 1; i <= n; ++i) 
      if (!flag[i] && (v == -1 || mincost[v] > mincost[i])) v = i;
    if (v == -1) break;
    flag[v] = 1;
    sum += mincost[v];
    for (int i = 1; i <= n; ++i)
      if (graph[v][i] != -1 && mincost[i] > graph[v][i]) mincost[i] = graph[v][i];
  }
  cout << sum << endl;
  return 0;
}

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

int main() {
    int i, j, n, res = 0;
    cin >> n;
    int a[n][n];

    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
            cin >> a[i][j];
        }
    }

    vector<int> v;
    set<int> cons;
    v.push_back(0);
    cons.insert(0);
    while (v.size() != n) {
        int nearest_node = 9999999;
        int nearest_distance = 9999999;
        for (i=0; i<v.size(); i++) {
            for (j=0; j<n; j++) {
                if ((a[v[i]][j] != -1) && (a[v[i]][j] < nearest_distance) && (cons.count(j) == 0)) {
                    nearest_node = j;
                    nearest_distance = a[v[i]][j];
                }
            }
        }
        res += nearest_distance;
        v.push_back(nearest_node);
        cons.insert(nearest_node);
    }
    cout << res << endl;
    return 0;
}

C++: #include <cstdio>
#include <vector>

int main(void){
	int n, min_edge, next_node, count, sum;
	scanf("%d", &n);
	std::vector< std::vector<int> > edges;
	std::vector<int> isUsed;
	edges.resize(n);
	isUsed.resize(n);
	for(int i=0;i<n;i++){
		isUsed[i]=-1;
		edges[i].resize(n);
		for(int j=0;j<n;j++){
			scanf("%d", &edges[i][j]);
		}
	}

	min_edge=3000;
	isUsed[0] = 1;
	count = 0;
	sum=0;
	while(count<4){
		for(int i=0;i<n;i++){
			if(isUsed[i]==1){
				for(int j=0;j<n;j++){
					if (isUsed[j]==-1 && edges[i][j]>-1 && edges[i][j]<min_edge){
						min_edge = edges[i][j];
						next_node = j;
					}
				}
			}
		}
		sum += min_edge;
		isUsed[next_node]=1;
		min_edge = 3000;
		count += 1;
	}
	printf("%d\n", sum);
	return 0;
}

C++: #include<vector>
#include<algorithm>
#include<queue>
#include<stdio.h>
using namespace std;
//结点数最大值 
const int maxn = 1000;
//结点权值最大值 
const int INFTY = 1000000;
//first是下一个结点位置,second是权值 
vector< pair<int,int> > g[maxn];
//parent是父节点,d是当前最短边,is是是否使用过 
int parent[maxn],d[maxn],is[maxn];
//结点数 
int n;
//初始化函数 
void Init(){
	for(int i = 0;i<n;++i){
		//父节点全设置为不存在 
		parent[i] = -1;
		//全设置为最大距离 
		d[i] = INFTY;
		//为输入边和权值做好准备 
		g[i].clear();
		//初始化各边使用状态 
		is[i] = false;
	}
}
int prim(){
	//最小生成树总权值 
	int ans = 0;
	//存储边 
	priority_queue< pair<int,int> >q;
	pair<int,int> work;
	//将0起点的存进去 
	for(int i = g[0].size()-1;i>=0;--i){
		work= g[0][i];
		parent[work.first] = 0;
		//最大堆变最小堆 
		work.second*=-1;
		swap(work.first,work.second);
		q.push(work);
	}
	is[0] = true;
	
	while(q.size()){
		work = q.top();q.pop();
		//to是结点号,v是权值 
		int to = work.second,v = -1*work.first;
		//距离过大或者目标节点已被标记则略过 
		if(d[to]<to||is[to])continue;
		is[to] = true;
		//展示选择的边 
		//printf("%d --> %d:%d\n",parent[to],to,v);
		ans+=v;
		//将当前结点to的边加进队列 
		for(int i  = g[to].size()-1;i>=0;--i){
			work= g[to][i];
			//如果权值比当前的还要大或者目标结点已经被标记过了则跳过 
			if(is[work.first]||d[work.first]<work.second)continue;
			parent[work.first] = to;
			work.second*=-1;
			swap(work.first,work.second);
			q.push(work);
		}
	}
	return ans;
}
int main(){
	int m,a,b,c;
	scanf("%d",&n);
	Init();
	for(int i = 0;i<n;++i){
		for(int j = 0;j<n;++j){
		scanf("%d",&a);
		if(a==-1)continue; 
		g[i].push_back(make_pair(j,a));
		}
	}
	printf("%d\n",prim());
	return 0;
}
//test:
/*
6 10
0 1 2
0 3 1
1 3 3
1 2 1
1 4 2
3 2 2
3 5 2
2 4 5
2 5 3
4 5 1
*/