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>
using namespace std;

int P[10010];
void init(int N);
int root(int a);
bool is_same_set(int a,int b);
void unite(int a,int b);

struct Node {
  int cost,start,goal;
  bool operator<( const Node& right ) const {
    return cost == right.cost ? start < right.start : cost < right.cost;
  }
};

vector<Node> Nodes;

int main() {
  int N;
  cin >> N;
  for (int i=0;i<N;++i) {
    for (int j=0;j<N;++j) {
      int buf;
      cin >> buf;
      if (buf!=-1) {
	Node bufNode = {buf,i,j};
	Nodes.push_back(bufNode);
      }
    }
  }
  sort(Nodes.begin(),Nodes.end());
  init(N); int totalCost = 0;
  for (int i=0;i<Nodes.size();++i) {
    if (is_same_set(Nodes[i].start,Nodes[i].goal)==false) {
      unite(Nodes[i].start,Nodes[i].goal);
      totalCost += Nodes[i].cost;
    }
  }
  cout << totalCost << endl;
}

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 (P[a] = root(P[a]));
}

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

void unite(int a,int b) {
  P[root(a)] = root(b);
}

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

int root(int i,vector<int> &parent){
    if(parent[i]==-1){
        //親がいない時
        return i;
    }else{
        int result=root(parent[i],parent);
        parent[i]=result; //パス圧縮
        return result;
    }
}

void unite(int x,int y,vector<int> &parent){
    if(root(x,parent)!=root(y,parent)){
        parent[root(y,parent)]=root(x,parent);
    }
}

bool issame(int x,int y,vector<int> &parent){
    return root(x,parent)==root(y,parent);
}

int main(){
    int n;
    cin >> n;
    //first:cost,second:(first:from,second:to)
    vector<pair<int,pair<int,int> > > bridges(n*n);

    //入力
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            int cost;
            if(cin >> cost,cost!=-1){
                bridges.push_back(pair<int,pair<int,int> >(cost,pair<int,int>(i,j)));
            }
        }
    }

    //sort
    sort(bridges.begin(),bridges.end());

    //島を管理する用
    vector<int> parents(n*n,-1);

    int sum=0;

    for(int i=0;i<bridges.size();++i){
        if(!issame(bridges[i].second.first,bridges[i].second.second,parents)){
            unite(bridges[i].second.first,bridges[i].second.second,parents);
            sum+=bridges[i].first;
        }
    }
    cout << sum;
}

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, i;
    int d[MAX], q[MAX], color[MAX];
    
    for ( i = 0; i < n; i++ ) {
        d[i] = INFTY;
        q[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];
                    q[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    for ( int i = 0; i < n; i++ ){
        if( q[i] != -1 ) sum += M[i][q[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;
}

C++: #include<iostream>
#include<climits>
using namespace std;
typedef struct{
  int a[2000];
  bool f;
}node;

node T[103];
int m;
int n;
int ans;
int cnt;
int num;
void route(){
  m=INT_MAX;
  if(cnt==n-1)return;
  for(int i=0;i<n;i++){
    if(T[i].f==true){
      for(int j=0;j<n;j++){
	if(m>T[i].a[j]){
	  if(T[j].f==false){
	    m=T[i].a[j];
	    num=j;
	  }
	}
      }
    }
  }
  ans+=m;
  cnt++;
  //  cout<<"m "<<m<<endl;
  T[num].f=true;
  route();
}
int main(){
  int key;
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>key;
      if(key==-1)T[i].a[j]=INT_MAX;
      else T[i].a[j]=key;
    }
  }
  T[0].f=true;
  route();
  cout<<ans<<endl;
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
const int INF = 10000;

template<typename ISTREAM>
void readWeightedGraphMatrix(ISTREAM& cin, vector< vector<int> >& weightedGraphMatrix) {
	size_t n;
	cin >> n;
	int weight;
	weightedGraphMatrix = vector< vector<int> >(n, vector<int>(n));
	for (size_t i = 0; i != n; ++i) {
		for (size_t j = 0; j != n; ++j) {
			cin >> weight;
			weightedGraphMatrix.at(i).at(j) = weight != -1 ? weight : INF;
		}
	}
}

int MST(const vector< vector<int> >& weightedGraphMatrix) {
	enum Color {
		WHITE, GRAY, BLACK//白は未訪問でTと隣接していない、。グレーはTと隣接状態だが未訪問、黒は訪問済み。木は閉路を持たないグラフなので、全域木の計算では
		//各点は1度だけ訪問されるはず。
	};
	const size_t n = weightedGraphMatrix.size();
	vector<Color> color(n,Color::WHITE);
	vector<int> d(n, INF);
	vector<int> p(n, INF);
	color.front() = Color::GRAY;
	d.front() = 0;
	int currentNode = 0;
	while (true) {
		int min = INF;
		int u;
		//TからV-Tへ接続する辺の重みの中で最小のものを求める。
		//同時に、その辺に接続する辺のT内のノードを求める。
		for (size_t i = 0; i != n; ++i) {
			if (color.at(i) == Color::BLACK || color.at(i) == Color::WHITE) continue;//訪問済みはスキップ
			if (d.at(i) >= min)continue;
			min = d.at(i);//TからV-Tの中のノードiへ向かう辺の重みの最小値を更新
			u = i;
		}

		
		if (min == INF) {
			//ノード０からアクセス可能なすべての点を訪問済みになったらMST構築完了
			break;
		}

		color.at(u) = Color::BLACK;//訪問

		for (int v = 0; v != n; ++v) {
			if (color.at(v) == Color::BLACK) continue;//訪問済みはスキップ
			if (weightedGraphMatrix.at(u).at(v) == INF ) continue;
			if (weightedGraphMatrix.at(u).at(v) >= d.at(v)) continue;
			d.at(v) = weightedGraphMatrix.at(u).at(v);
			p.at(v) = u;
			color.at(v) = Color::GRAY;

		}
	
	}
	int sum = 0;
	for (int i = 0; i != n; ++i) {
		if (p.at(i) == INF)continue;
		sum += weightedGraphMatrix.at(i).at(p.at(i));
	}
	return sum;
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readWeightedGraphMatrix(cin, graphList);
	cout << MST(graphList) << endl;
}


int main()
{


	execute(cin);



	return 0;
}