Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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
*/