Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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;
}