Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

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

int main(){
  int n;
  int G[100][100],d[100];
  int minc,mi,i;
  bool flg[i];
  int sum=0;
  
  cin >> n;
  for (i=0;i<n;i++){
    for (int j=0;j<n;j++){
      cin >> G[i][j];
      if (G[i][j]==-1) G[i][j]=INF;
    }
    d[i]=INF;
    flg[i]=false;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (!flg[i]&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flg[mi]=true;
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (!flg[i]&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    sum+=d[i];
  }
  cout << sum<<endl;
  
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<stack>
#include<vector>
#define INFTY 10000
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

int color[120],d[120];
int M[120][120];
int n;

int start(int x){
  d[x]=0;
  while(true){
    int minimumd=INFTY,u;
    for(int i=0;i<n;i++){
      if(color[i]!=BLACK && minimumd>d[i]){
        minimumd=d[i];
        u=i;
      }
    }
    if(minimumd==INFTY) break;
    color[u]=BLACK;
    for(int i=0;i<n;i++){
      if(d[i]>M[u][i] && color[i]!=BLACK){
        d[i]=M[u][i];
        color[i]=GRAY;
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    sum+=d[i];
  }
  return sum;
}

int main(){
  int temp;
  cin>>n;
  for(int i=0;i<n;i++){
    d[i]=INFTY;
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>temp;
      M[i][j]=(temp==-1)? INFTY : temp; 
    }
  }
  d[0]=0;
  int ans=start(0);
  cout<<ans<<endl;
}

C++: #include<iostream>
using namespace std;
#define INFTY (1 << 30)

enum Color {
	WHITE,
	GRAY,
	BLACK,
};

void Prim(int verValWeight[][100], int verNum) {
	int distance[100];
	Color color[100];
	int parent[100];
	for (int i = 0; i < verNum; i++) {
		color[i] = WHITE;
		distance[i] = INFTY;
		parent[i] = -1;
	}
	distance[0] = 0;

	while (true) {
		int min_cost = INFTY;
		int subScript;
		for (int i = 0; i < verNum; i++) {
			if (color[i] != BLACK && distance[i] < min_cost) {
				min_cost = distance[i];
				subScript = i;
			}
		}

		if (min_cost == INFTY)
			break; //end

		color[subScript] = BLACK;

		for (int i = 0; i < verNum; i++) {
			if (color[i] != BLACK && verValWeight[subScript][i] != INFTY) {
				if (verValWeight[subScript][i] < distance[i]) {
					distance[i] = verValWeight[subScript][i];
					parent[i] = subScript;
					color[i] = GRAY;
				}
			}
		}
	}

	int weight_sum = 0;
	for (int i = 0; i < verNum; i++)
		weight_sum += distance[i];
		//if (parent[i] != -1)
		//	weight_sum += verValWeight[i][parent[i]];
	cout << weight_sum << endl;

}

int main() {
	int verNum;
	int verValWeight[100][100];
	int parent[100];

	cin >> verNum;
	for (int i = 0; i < verNum; i++)
		for (int j = 0; j < verNum; j++) {
			cin >> verValWeight[i][j];
			if (verValWeight[i][j] == -1) verValWeight[i][j] = INFTY;
		}

	Prim(verValWeight, verNum);

	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef pair< int, int > Pi;
const int INF = 1 << 30;
int info[100][100];
bool used[100];

int prim(const int& V){
  fill_n( used, V, false);
  priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  que.push(Pi( 0, 0));
  int ret = 0;
  while(!que.empty()){
    Pi p = que.top();
    que.pop();
    if(used[p.second]++) continue;
    ret += p.first;
    for(int i = 0 ; i < V ; i++){
      if(~info[p.second][i]) que.push( Pi( info[p.second][i], i));
    }
  }
  return ret;
}

int main(){
  int N;
  scanf("%d", &N);
  for(int i = 0; i < N; i++){
    for(int j = 0 ; j < N ; j++){
      scanf("%d", &info[i][j]);
    }
  }
  printf("%d\n", prim(N));
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   for(int i=0;i<(n);++i)
#define SORT(c)    sort((c).begin(),(c).end())
#define CLR(a)     memset((&a), 0 ,sizeof(a))//clear memory
#define dump(x)    cerr << #x << " = " << (x) << endl;//debug
#define debug(x)   cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define EPS        1e-10//sample:if((double)>=(double)+EPS)
//cin,cout release
//std::cin.tie(0);
//ios::sync_with_stdio(false);
#define MAX 100
using namespace std;
int n,A[MAX][MAX];
bool r[101][101]={};
#define WHITE 0
#define GRAY 1
#define BLACK 2
int prim(){
  int u;
  int d[MAX],pi[MAX],color[MAX];
  int mincost;
  rep(i,n){
    d[i]=INT_MAX;
    pi[i]=-1;
    color[i]=0;
  }
  d[0]=0;

  while(1){
    mincost=INT_MAX;
    u=-1;
    rep(i,n)
      if(mincost>d[i]&&color[i]!=BLACK){
	u=i;
	mincost=d[i];
      }
    if(u==-1)break;
    color[u]=BLACK;
    rep(v,n){
      if(color[v]!=BLACK&&A[u][v]!=INT_MAX){
	if(d[v]>A[u][v]){
	  d[v]=A[u][v];
	  pi[v]=u;
	  color[v]=GRAY;
	}
      }
    }
  }
  int sum=0;
  rep(i,n)if(pi[i]!=-1)sum+=A[i][pi[i]];
  return sum;
}
int main(){
  int a;
  cin>>n;
  rep(i,n)rep(j,n){
    cin>>a;
    if(a==-1)A[i][j]=INT_MAX;
    else A[i][j]=a;
  }
  cout<<prim()<<endl;

  return 0;
}