Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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