Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

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