Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			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
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
using namespace std;

struct Edge{
  int src,dst;
  int weight;
  Edge(int src,int dst,int weight):
    src(src),dst(dst),weight(weight){}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

bool operator < (const Edge &m,const Edge &n){
  return m.weight!=n.weight ? m.weight>n.weight :
    m.src!=n.src ? m.src>n.src : m.dst>n.dst;
}

struct UnionFind{
  vector<int> data;
  UnionFind(int size):
    data(size,-1){}

  int root(int x){
    return data[x]<0 ? x :
      data[x]=root(data[x]);
  }

  void unionset(int x,int y){ //bool->void
    x=root(x);y=root(y);
    if(x!=y){
      if(data[y]<data[x])
	swap(x,y);
      data[x]+=data[y];
      data[y]=x;
    }
    //return x!=y;
  }

  bool findset(int x,int y){
    return root(x)==root(y);
  }
};

pair<int,Edges> MinimumSpinningForest(Graph g){
  int n=g.size();
  UnionFind uf(n);
  priority_queue<Edge> que;

  for(int i=0;i<n;i++){
    for(Edges::iterator l=g[i].begin();l!=g[i].end();l++){
      if(i<(*l).dst)
	que.push(*l);
    }
  }

  int sum=0;
  Edges select;

  while((int)select.size()<n-1 && !que.empty()){
    Edge tmp=que.top();que.pop();

    if(!uf.findset(tmp.src,tmp.dst)){
      uf.unionset(tmp.src,tmp.dst);
      select.push_back(tmp);
      sum+=tmp.weight;
    }
  }

  return pair<int,Edges>(sum,select);
}

int main(int argc,char* argv[]){
  Graph g;
  int n,x;
  pair<int,Edges> p;

  cin>>n; 
  for(int i=1;i<=n;i++){
    for(int l=1;l<=n;l++){
      cin>>x;
      Edge tmp(i,l,x);
      Edges input;input.push_back(tmp);
      g.push_back(input);
    }
  }

  p=MinimumSpinningForest(g);

  cout<<p.first<<endl;
  return 0;
}

C++: #include<bits/stdc++.h>
#define N 100
#define INFTY (1<<21)
#define WH 0
#define GR 1
#define BL 2
using namespace std;

vector<int> color(N),d(N),p(N);
vector<vector<int>> M(N,vector<int>(N));
int n;

int prim(){
	int u,mincost;
	for(int i=0;i<n;i++){
		color.at(i) = WH;
		d.at(i) = INFTY;
		p.at(i) = -1;
	}
	d.at(0) = 0;
	
	while(1){
		mincost = INFTY;
		u = -1;
		for(int i=0;i<n;i++){
			if(color.at(i) != BL && d.at(i) < mincost){
				mincost = d.at(i);
				u = i;
			}
		}
		if(u == -1) break;
		
		color.at(u) = BL;
		
		for(int v=0;v<n;v++){
			if(color.at(v) != BL && M.at(u).at(v) != INFTY){
				if(M.at(u).at(v) < d.at(v)){
					d.at(v) = M.at(u).at(v);
					p.at(v) = u;
					color.at(v) = GR;
				}
			}
		}
	}
	int sum=0;
	for(int i=0;i<n;i++){
		if(p.at(i) != -1) sum += M.at(i).at(p.at(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;
			if(e == -1) M.at(i).at(j) = INFTY;
			else M.at(i).at(j) = e;
		}
	}
	cout << prim() << endl;
}

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 a, min;
   int d[MAX], p[MAX], color[MAX];
  
   for ( int i = 0; i < n; i++) {
      d[i] = INFTY;
      p[i] = -1;
      color[i] = WHITE;
   }
 
   d[0] = 0;
 
   while ( 1 ) {
      min = INFTY;
      a = -1;
 
      for ( int i = 0; i < n; i++ ) {
         if ( min > d[i] && color[i] != BLACK ) {
            a = i;
            min = d[i];
         }
      }
 
      if ( a == -1 ) break;
      color[a] = BLACK;
      for ( int v = 0; v < n; v++ ) {
         if ( color[v] != BLACK && M[a][v] != INFTY ) {
            if ( d[v] > M[a][v] ) {
               d[v] = M[a][v];
               color[v] = GRAY;
            }
         }
      }
   }
 
   int sum = 0;
   for ( int i = 0; i < n; i++ ) {
      if ( p[i] != -1 ) sum = 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) ? INFTY : e;
      }
   }
 
   cout << prim() << endl;
 
   return 0;
}

C++: #include<iostream>
using namespace std;
#define MAX 100
#define INFTY 2001
#define WHITE 0
#define GRAY 1
#define BLACK 2

int M[MAX][MAX], d[MAX];

void prim(int n){
  int color[MAX], u;

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

  while(true){
    int mincost = INFTY;
    for(int 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(int v=0; v<n; v++){
      if(color[v] != BLACK && M[u][v] != INFTY){
        if(M[u][v] < d[v]){
          d[v] = M[u][v];
          //p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
}

int main(void){
  int n;
  cin >> n;
  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
      cin >> M[i][j];
      if(M[i][j] == -1) M[i][j] = INFTY;
    }
  }

  prim(n);

  // 最小全域木の辺の重みの総和を計算
  int u = 0;
  int d_sum = 0;
  for(int i=0; i<n; i++){
    if(true){
      d_sum += d[i];
    }
  }
  cout << d_sum << endl;
  return 0;
}

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

#define N 105
int n;

int g[N][N];
int mini;
int weight[N];
bool us[N];

int prim(){
  int v;
  int ans=0;
  weight[1]=0;
  
  while(true){
    v=-1;
   
    for(int i=1;i<=n;i++){
      if(us[i]!=true && (v==-1 ||weight[i]<weight[v]))v=i;
    }

    if(v==-1)break;
    us[v]=true;
    ans+=weight[v];

    for(int i=1;i<=n;i++){
      if(weight[i]>g[v][i])weight[i]=g[v][i];
      else weight[i]=weight[i];
    }
  }
  return ans;
}


int main(){

  //load n
  cin >>n;

  //load data
  for(int i=1;i<=n;i++){
    weight[i]=1000;
    us[i]=false;
    for(int j=1;j<=n;j++){
      scanf("%d",&g[i][j]);
      if(g[i][j]== -1)g[i][j]=1000;
      //    cout <<g[i][j]<<" ";
    }
    //    cout <<endl;
  }




  //print
  cout <<prim()<<endl;

  return 0;

}