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>
#include <vector>
#include <algorithm>

struct edge {
	int x;
	int y;
	int cost;
	edge(int x, int y, int cost) { this->x = x; this->y = y; this->cost = cost; }
};

bool operator<(const edge& e, const edge& f) { return e.cost < f.cost; }
bool operator<=(const edge& e, const edge& f) { return e.cost <= f.cost; }
bool operator>(const edge& e, const edge& f) { return e.cost > f.cost; }
bool operator>=(const edge& e, const edge& f) { return e.cost >= f.cost; }
bool operator==(const edge& e, const edge& f) { return e.cost == f.cost; }
bool operator!=(const edge& e, const edge& f) { return e.cost != f.cost; }

int root(int* par, int x)
{
	if (par[x] == x) return x;
	else return root(par, par[x]);
}
	
bool isSame(int* par, int x, int y)
{
	return root(par, x) == root(par, y);
}

void unite(int* par, int x, int y)
{
	if (!isSame(par, x, y)) par[root(par, x)] = root(par, y);
}

int main()
{
	int n; std::cin >> n;
	
	int *par = new int[n];
	for (int i=0; i<n; i++) par[i] = i;
	
	std::vector<edge> edge_list;
	
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) {
			int cost;
			std::cin >> cost;
			if (i < j && cost >= 0) edge_list.push_back( edge(i, j, cost) );
		}
	}
	
	std::sort(edge_list.begin(), edge_list.end());
	
	int totalcost = 0;
	int num_of_edges = 0;
	for (std::vector<edge>::iterator it = edge_list.begin(); it != edge_list.end(); ++it) {
		if (isSame(par, (*it).x, (*it).y)) continue;
		unite(par, (*it).x, (*it).y);
		totalcost += (*it).cost;
		num_of_edges++;
		// std::cout << (*it).x << "," << (*it).y << " " << (*it).cost << std::endl;
		if (num_of_edges == n-1) break;
	}
	
	std::cout << totalcost << std::endl;

	delete[] par;
	
	return 0;
}

C++: #include<stdio.h>
static const int INFTY = (1<<21);
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2


int n,v,i,M[MAX][MAX];
int prim(void);

int main(){
  int j,e;
  scanf("%d",&n);

  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&e);
      M[i][j]=(e==-1)?INFTY:e;
    }
  }
  printf("%d",prim());
  return 0;
}

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

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

  d[0]=0;

  while(1){
    minv=INFTY;
    u=-1;
    for(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(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;
        }
      }
    }
  }
  for(i=0;i<n;i++){
    if(p[i]!=-1)sum+=M[i][p[i]];
  }
  return sum;
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
//#include<operator>

using namespace std;


class UFT{
public:
  int P[10010];//その頂点の親を格納する

  //初めは全ての頂点はバラバラで違うグループに属する
  void init(int N){
    for(int i=0;i<N;i++) P[i]=i;
  }

  int root(int a){
    if(P[a]==a) return a;
    return root(P[a]);
  }

  bool is_same_set(int a,int b){
    return root(a)==root(b);
  }

  void unite(int a,int b){
    //aのルートだったもののルートをbのルートにする
    P[root(a)]=root(b);
  }
};

bool is_same_set_all(UFT a,int n){
  for(int i=0;i<n-1;i++){
    if(!(a.is_same_set(i,i+1))){return false;}
  }
  return true;
}

class edge{
public:
  int from_Node;
  int to_Node;
  int cost;
};

//本来は<さえオーバーロードすれば大丈夫が四つのオペレータをオーバロードしなければならなかった(何故？？)
bool operator< (const edge& e1,const edge& e2){
  return e1.cost < e2.cost;
}


bool operator> (const edge& e1,const edge& e2){
  return e1.cost > e2.cost;
}


bool operator<= (const edge& e1,const edge& e2){
  return e1.cost <= e2.cost;
}

bool operator>= (const edge& e1,const edge& e2){
  return e1.cost >= e2.cost;
}


int main(){
  #if 0
  このようにデバッグができる(比較演算子のオーバーロード)
  edge a={0,1,2},b={1,2,3};
  if(a<b){cout << "a<b" << endl;}
  if(b<a){cout << "b<a" << endl;}
  if(a>b){cout << "a>b" << endl;}
  if(a<=b){cout << "a<=b" << endl;}
  #endif


  int n;cin >> n;
  priority_queue<
  edge,//要素の型
  vector<edge>,//内部コンテナはvectorで(デフォ)
  greater<edge>//昇順
  > ve;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      edge e;int c;
      cin >> c;
      if(c!=-1 and i<j){
        e.from_Node=i;e.to_Node=j;e.cost=c;
        ve.push(e);
      }
    }
  }
  UFT G;G.init(n);
  int co=0;
  while(!(is_same_set_all(G,n))){
    if(!(G.is_same_set(ve.top().from_Node,ve.top().to_Node))){
      G.unite(ve.top().from_Node,ve.top().to_Node);
      co+=ve.top().cost;
      ve.pop();
    }else{
      ve.pop();
    }
  }
  cout << co << endl;

}

C++: #include <bits/stdc++.h>
#define MAX 20000
using namespace std;

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n; cin >> n;
  vector< vector<int> > v(n, vector<int>(n));
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> v[i][j];
      if(v[i][j] == -1) v[i][j] = MAX;
    }
  }

  vector<int> cost(n, MAX), used(n, false);
  cost[0] = 0;
  int ans = 0;
  int t;

  while(true){
    t = -1;
    for(int i = 0; i < n; i++){
      if(!used[i] && (t == -1 || cost[t] > cost[i])) t = i;
    }
    if(t == -1) break;
    used[t] = true;
    ans += cost[t];

    for(int i = 0; i < n; i++){
      cost[i] = min(cost[i], v[t][i]);
    }
  }

  cout << ans << endl;

  return 0;
}