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<bits/stdc++.h>
using namespace std;

typedef pair< int , int > P;
struct Edge {
  int to,cost;
  Edge(){}
  Edge(int to,int cost):to(to),cost(cost){};
};
vector< vector< Edge > > Graph;
int V,E;
int prim(){
  int total = 0;
  vector< bool > used(Graph.size(),false);
  priority_queue< P,vector< P >,greater< P > > que;
  que.push(P(0,0));
  while(!que.empty()){
    int cost = que.top().first;
    int now = que.top().second;
    que.pop();
    if(used[now]) continue;
    used[now] = true;
    total += cost;
    for(int i=0;i<Graph[now].size();i++){
      que.push(P(Graph[now][i].cost,Graph[now][i].to));
    }
  }
  return total;
}

int main(){
  cin >> V;
  Graph.resize(V+1);
  /*
  for(int i=0;i<E;i++){
    int s,t,d;
    cin >> s >> t >> d;
    Graph[s].push_back(Edge(t,d));
    Graph[t].push_back(Edge(s,d));
    }*/
  for(int i=0;i<V;i++){
    for(int j=0;j<V;j++){
      int d; cin >> d;
      if(d != -1){
	Graph[i].push_back(Edge(j,d));
	Graph[j].push_back(Edge(i,d));
      }
    }
  }
  cout << prim() << endl;
}

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

int main(){
  vector<vector<int> > graph;
  vector<int> master_node;
  vector<int> distance;
  vector<int> to_neighbor;
  int n;

  cin >> n;
  n++;
  graph.resize(n);
  master_node.resize(n);
  distance.resize(n);
  to_neighbor.resize(n);
  for(int i=1;i<n;i++){
    graph[i].resize(n);
    master_node[i]=0;
    distance[i]=INT_MAX;
    to_neighbor[i]=INT_MAX;
  }

  for(int i=1;i<n;i++){
    for(int j=1;j<n;j++){
      int input;

      cin >> input;
      graph[i][j]=(input!=-1?input:INT_MAX);
    }
  }


  int from=1;

  master_node[1]=1;
  distance[1]=0;
  for(int i=1;i<n;i++){
    to_neighbor[i]=(graph[1][i]<to_neighbor[i]?graph[1][i]:to_neighbor[i]);
  }
  while(true){
    int min_distance=INT_MAX,to;

    for(int i=1;i<n;i++){
      if((to_neighbor[i]<min_distance)&&(master_node[i]==0)){
        min_distance=to_neighbor[i];
	to=i;
      }
    }
    if(min_distance==INT_MAX){
      break;
    }else{
      master_node[to]=from;
      distance[to]=min_distance;
      from=to;
      to_neighbor[from]=INT_MAX;
      for(int i=1;i<n;i++){
        to_neighbor[i]=(graph[from][i]<to_neighbor[i]?graph[from][i]:to_neighbor[i]);
      }
    }
  }


  int sum=0;
  
  for(int i=1;i<n;i++){
    sum+=distance[i];
  }
  cout << sum << endl;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];

int prim() {
	int d[MAX],
		p[MAX] = { -1 },
		color[MAX] = {};//WHITE
	fill(d, d + MAX, INF);
	d[0] = 0;

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

		if (minv == INF) { break; }

		color[u] = BLACK;

		for (int v = 0; v < n; v++) {
			if (color[v] != BLACK&&M[u][v] != INF) {
				if (M[u][v] < d[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 a; cin >> a;
			M[i][j] = (a == -1) ? INF : a;
		}
	}

	cout << prim() << endl;

	return 0;
}

C++: #include <iostream>
#include <climits>
#include <queue>

using namespace std;
static const int MAX=100;
static const int WHITE=1;
static const int GRAY=2;
static const int BLACK=3;

int n,M[MAX+10][MAX+10];

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

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

    d[0]=0;

    while(1)
    {
        minv=INT_MAX;
        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]!=INT_MAX)
        {
            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++)
      for(int j=0;i<n;j++)
    {
        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)? INT_MAX:e;
    }

    cout<<prim()<<endl;
    return 0;
}