Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
static const int MAX = 100;
static const int INFTY = (1 << 30);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int n, M[MAX][MAX]; //nは要素数、Mは隣接行列
int prim()
{
    int u, minv;                    //uは最短距離の相手、minvは最短距離
    int d[MAX], p[MAX], color[MAX]; //dは始点からの距離、pは親,colorは訪問状態
    rep(i, 0, n)                    //初期設定
    {
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE; //未訪問
    }
    d[0] = 0; //始点のdは0
    while (1)
    {
        minv = INFTY;
        u = -1;
        rep(i, 0, n) //未完了の点のうち始点からの最短距離は？
        {
            if (minv > d[i] && color[i] != BLACK) //未完了の点がある
            {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1)
            break;
        color[u] = BLACK;
        rep(i, 0, n)
        {
            if (color[i] != BLACK && M[u][i] != INFTY)
            {
                if (d[i] > M[u][i])
                {
                    d[i] = M[u][i];
                    p[i] = u;
                    color[i] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    rep(i, 0, n)
    {
        if (p[i] != -1)
            sum += M[i][p[i]];
    }
    return sum;
}
int main()
{
    cin >> n;
    rep(i, 0, n)
    {
        rep(j, 0, n)
        {
            int e;
            cin >> e;
            M[i][j] = (e == -1) ? INFTY : e;
        }
    }
    cout << prim() << endl;
}

C++: 

#include <iostream>
using namespace std;
#define INFTY  (1 << 30)//极大值
#define NIL  -1
const int TOTAL = 101;
int totalnode,color[TOTAL], matrix[TOTAL][TOTAL], DistanceBetweenParent[TOTAL], parent[TOTAL];//颜色0白色，1为灰色，2是黑色
void includeMinimumSpanningTree()
{
	int u,min;
	DistanceBetweenParent[0] = 0;//这个点到其父节点的距离为0
	parent[0] = -1;//根的父节点为-1
	while (1)
	{
		min = INFTY;
		for (int i = 0; i < totalnode; i++)//找出非黑色同时到父节点加权值最少的节点
			if (color[i] != 2 && DistanceBetweenParent[i] < min)//第一次只有根
				min = DistanceBetweenParent[i], u = i;//以u寻找连接它的其他点
		color[u] = 2;//u的颜色为黑色
		if (min == INFTY)break;//寻找结束
		for (int i = 0; i < totalnode; i++)//寻找相连的点
		{
			if (color[i] != 2 && matrix[u][i]!= -1)//未发现且存在边
			{
				if (matrix[u][i] < DistanceBetweenParent[i])//当前点连接父节点的加权值大于新的加权值
				{
					color[i] = 1;//灰色
					DistanceBetweenParent[i] = matrix[u][i];
					parent[i] = u;
				}
			}
		}
	}
}
int main()
{	
	int inputdata,minmum=0;
	cin >> totalnode;
	for (int i = 0; i < totalnode; i++)color[i] = 0;//颜色初始化
	for(int i = 0; i < totalnode; i++)DistanceBetweenParent[i] = INFTY;//将距离初始化为最大值
	for (int i = 0; i < totalnode; i++)//顶点i 到顶点j的距离，不存在的为-1；
		for (int j = 0; j < totalnode; j++)
			cin >> inputdata, matrix[i][j] = inputdata;
	includeMinimumSpanningTree();
	for (int i = 0; i < totalnode; i++)
		minmum += DistanceBetweenParent[i];
	cout << minmum << endl;

}

C++: 
#include<stdio.h>

#define MAX 100
#define INFINITY 10000

int n;
int M[MAX][MAX], color[MAX], d[MAX], p[MAX];

enum {
    WHITE,
    GRAY,
    BLACK
};

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

    while (true) {
        int min = INFINITY;
        int target = -1;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min && color[i] != BLACK) {
                min = d[i];
                target = i;
            }
        }
        if (target == -1) break;

        color[target] = BLACK;

        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && M[target][v] > 0) {
                if(M[target][v] < d[v]) {
                    d[v] = M[target][v];
                    p[v] = target;
                    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() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &M[i][j]);
        }
    }
    printf("%d\n", prim());
    return 0;
}

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

#define INF 1<<21
#define BLACK 0
#define GRAY 1
#define WHITE 2

int prim(int n, int **A, int *d, int *p) {
  int *color = new int[n];

  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    d[i] = INF;
  }

  d[0] = 0;
  p[0] = -1;

  while (true) {
    int mincost = INF;
    int u;
    for (int i = 0; i < n; i++) {
      if ((color[i] != BLACK) && (d[i] < mincost)) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INF) {
      break;
    }

    color[u] = BLACK;

    for (int v = 0; v < n; v++) {
      if ((color[v] != BLACK) && (A[u][v] != INF)) {
        if (A[u][v] < d[v]) {
          d[v] = A[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }

  }

  delete color;

  return 0;
}

int main() {
  int n;
  cin >> n;

  int **A = new int*[n];
  for (int i = 0; i < n; i++) {
    A[i] = new int[n];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int w;
      cin >> w;
      if (w == -1) {
        A[i][j] = INF;
      }
      else {
        A[i][j] = w;
      }
    }
  }

  int *d = new int[n];
  int *p = new int[n];
  prim(n, A, d, p);

  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += d[i];
  }

  cout << sum << endl;

  for (int i = 0; i < n; i++) {
    delete[] A[i];
  }
  delete[] A;

  delete d;
  delete p;

  return 0;
}