Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

C++: #include<iostream>
using namespace std;
static const int MAX=100;
static const int INF=(1<<21);
static const int WH=0;
static const int GRY=1;
static const int BL=2;

int n, MA[MAX][MAX];

int prim(){
  int  u, minV;
  int d[MAX], p[MAX], color[MAX];

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

  d[0]=0;

  while(1)
  {
    minV=INF;
    u = -1;
    for(int i=0; i<n; i++)
    {
      if(minV > d[i] && color[i] != BL)
      {
        u=i;
        minV=d[i];
      }
    }
    if(u == -1) break;
    color[u]=BL;
    for(int v=0; v<n; v++)
    {
      if(color[v] != BL && MA[u][v] != INF)
      {
        if(d[v] > MA[u][v])
        {
          d[v]=MA[u][v];
          p[v]=u;
          color[v]=GRY;
        }
      }
    }
  }
  int sum=0;
  for(int i=0; i<n; i++)
  {
    if(p[i] != -1) sum += MA[i][p[i]];
  }

  return sum;
}

int main(){
  cin >> n;

  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++)
    {
      int e;
      cin >> e;
      MA[i][j] = (e == -1) ? INF : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

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

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 114514

int x[10000][10000];
int color[10000];
int MIN[10000];
int sum;
int sum_min = INF;

int main() {
	int n, a, b, c;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> x[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			MIN[j] = INF;
			color[j] = WHITE;
		}
		b = INF;
		a = n;
		color[i] = GRAY;
		MIN[i] = 0;
		while (a != 0) {
			b = INF;
			for (int j = 0; j < n; j++) {
				if (color[j] == GRAY) {
					if (b > MIN[j]) {
						b = MIN[j];
						c = j;
					}
				}
			}
			color[c] = BLACK;
			a -= 1;
			for (int k = 0; k < n; k++) {
				if (color[k] != BLACK && x[c][k] != -1) {
					color[k] = GRAY;
					MIN[k] = min(x[c][k], MIN[k]);
				}
			}
		}
		sum = 0;
		for (int j = 0; j < n; j++) {
			sum += MIN[j];
		}
		sum_min = min(sum_min, sum);
	}
	cout << sum_min << endl;
	return 0;
}

C++: #include <stdio.h>
#define INFTY 1000000
#define M -1
#define WHITE 0
#define GRAY  1
#define BLACK 2
#define N 100
int prim(void);
int n,A[N][N];

int main()
{
  int i,j,k;

  scanf("%d",&n);
  for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
        {
          scanf("%d",&k);
          if(k==M) A[i][j]=INFTY;
        }
    }

  printf("%d\n",prim());
  return 0;
}

int prim()
{
  int d[N],p[N],color[N];
  int i,v,u,mincost,sum=0;

  for(i=0;i<n;i++)
    {
      d[i]=INFTY;
      p[i]=M;
      color[i]=WHITE;
    }
  d[0]=0;
while(1)
    {
      mincost=INFTY;
      u=M;
      for(i=0;i<n;i++)
        {
          if(color[i]!=BLACK&&d[i]<mincost)
            {
              u=i;
              mincost=d[i];
            }
        }
      if(u==M) break;
      color[u]=BLACK;
      for(v=0;v<n;v++)
        {
          if(color[v]!=BLACK&&A[u][v]!=INFTY)
            {
              if(d[v]>A[u][v])
                {
                  d[v]=A[u][v];
                  p[v]=u;
                  color[v]=GRAY;
                }
            }
        }
    }
  for(i=0;i<n;i++)
    {
      if(p[i]!=M) sum+=A[i][p[i]];
    }
  return sum;

C++: #include <iostream>
#define N 100
#define INFINITY 200000000
using namespace std;
void prim();
int M[N][N],d[N],p[N],u,n,mincost,i,j;
string color[N];
int main(){
  cin >> n;
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      cin >> M[i][j];
    }
  }
  prim();
  return 0;
}
void prim(){
  for(i=0;i<n;i++) color[i]="WHITE",d[i]=INFINITY;
  d[0]=0;
  p[0]=-1;
  while(1){
    mincost=INFINITY;
    for(i=0;i<n;i++){
      if(color[i]!="BLACK"&&d[i]<mincost){
	mincost=d[i];
	u=i;
      }
    }
    if(mincost==INFINITY) break;
    color[u]="BLACK";
    for(i=0;i<n;i++){
      if(color[i]!="BLACK"&&M[u][i]!=-1){
	if(M[u][i]<d[i]){
	  d[i]=M[u][i];
	  p[i]=u;
	  color[i]="GLAY";
	}
      }
    }
  }
  int sum=0;
  for(i=0;i<n;i++) sum+=d[i];
  cout << sum << endl;
}

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

static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;


int n, M[MAX][MAX];

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

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

    d[0] = 0;

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

    cout << prim() << endl;

    return 0;
}