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>

using namespace std;


class Graph{
private:

  int size;
  int **graph;
  int *state;

public:

  Graph(int size){
    int i;
    int j;
    this->size = size;
    this->state = new int[size];
    this->graph = new int*[size];
    for(i = 0;i < size;i++){
      this->graph[i] = new int[size];
    }
    for(i = 0;i < this->size;i++){
	this->state[i] = 0;
      }
  }

  ~Graph(){
    int i;
    for(i = 0;i < this->size;i++){
      delete[] this->graph[i];
    }
    delete[] this->graph;
  }

  int getMinimum(){
    int i;
    int j;
    int min;
    int res = 0;

    for(i = 0;i < this->size;i++){
      min = 0;

      for(j = 1;j < this->size;j++){
	if(this->graph[i][j] != -1 && this->state[j] == 0){
	  if(this->graph[i][j] <= this->graph[i][min] 
	     ||	this->graph[i][min] == -1){
	    min = j;
	  }
	}
      }

      if(state[min] == 0){
	state[min] = 1;
	res += graph[i][min];
      }
    }
    return res;
  }

  void setValue(int x,int y,int value){
    this->graph[x][y] = value;
  }

};

int main(){

  int i;
  int j;
  int n;
  int a;

  Graph *graph;

  cin >> n;

  graph = new Graph(n);

  for(i = 0;i < n;i++){
    for(j = 0;j < n;j++){
      cin >> a;
      graph->setValue(i,j,a);
    }
  }

  cout << graph->getMinimum() << endl;

  delete graph;

  return 0;
}

C++: #include <iostream>
#define NMAX 100
#define AMAX 2001

using namespace std;

enum state{
  WHITE,
  BLACK
};

int main(){
  int A[NMAX][NMAX];
  int n; cin>>n;
  state s[NMAX];
  
  for(int r=0;r<n;r++){
    for(int c=0;c<n;c++){
      cin>>A[r][c];
      if(A[r][c]==-1) A[r][c]=AMAX;
    }
  }

  for(int i=0;i<n;i++) s[i]=WHITE;
  s[0]=BLACK;
  
  int sum=0;

  while(1){
    bool flag=true;
    for(int i=0;i<n;i++){
      if(s[i]!=BLACK) flag=false;
    }
    if(flag) break;

    int min=AMAX;
    int minr,minc;
    for(int r=0;r<n;r++){
      for(int c=0;c<n;c++){
        if(s[r]==BLACK && s[c]!=BLACK && A[r][c]<min){
          min=A[r][c];
          minr=r;minc=c;
        }
      }
    }
    s[minr]=BLACK;s[minc]=BLACK; 
    sum+=min;
  }
  cout<<sum<<endl;
}

C++: #include<iostream>
#include<vector>

int main() {
	int num;
	std::cin >> num;

	std::vector<std::vector<int> > v(num);

	for (int i = 0; i < num; i++) {
		for (int j = 0; j < num; j++) {
			int n;
			std::cin >> n;
			v[i].push_back(n);
		}
	}

	std::vector<int> n(v.size(), 0);
	std::vector<int> color(v.size(), 0);
	int count = 0, color_count = 1;
	for (int i = 1; i <= 2000; i++) {
		for (size_t j = 0; j < v.size(); j++) {
			for (size_t k = 0; k < v.size(); k++) {
				if (v[j][k] == i && ((color[k] == 0 || color[j] == 0) || (color[k] != color[j]))) {
					if (color[k] == 0) {
						if (color[j] != 0) {
							color[k] = color[j];
						} else {
							color[k] = color[j] = color_count;
							color_count++;
						}
					} else {
						if (color[j] == 0) {
							color[j] = color[k];
						} else {
							for (int l = 0; l < color.size(); l++) {
								if (color[l] == color[j]) {
									color[l] = color[k];
								}
							}
						}
					}
					count += i;
				}
			}
		}
	}

	std::cout << count << std::endl;

	return 0;
}

C++: #include <iostream>

using namespace std;

static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int LEN=100;
static const int INF=(1<<21);

int n,M[LEN][LEN];


int prim()
{
    int u,minc;
    int d[LEN],node[LEN],color[LEN];
    for(int i=0; i<n; i++){
        node[i]=-1;
        d[i]=INF;
        color[i]=WHITE;
    }
    d[0]=0;

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

        if(u==-1) break;
        color[u]= BLACK;

        for(int i=0;i<n;i++){
            if(color[i]!=BLACK && M[u][i]!=INF){
                if(M[u][i]<d[i]){
                    d[i]=M[u][i];
                    node[i]=u;
                    color[i]=GRAY;
                }
            }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++){
        if(node[i]!=-1) sum+=M[i][node[i]];
    }
    return sum;
}



int main()
{
    cin>>n;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            int k;
            cin>>k;
            M[i][j]=(k==-1?INF:k);
        }
    }
    cout<<prim()<<endl;

    return 0;
}

C++: #include<cstdio>
#include<stdlib.h>
#include<iostream>

using namespace std;

const int maxn = 105;

int M[maxn][maxn];
int visit[maxn];
int d[maxn];
int p[maxn];
int n;


int main(){
	cin >> n;
	for(int i = 0;i < n;i++){
		visit[i] = false;
		d[i] = 0x3f3f3f;
		for(int j = 0;j < n;j++){
			cin >> M[i][j];
		}
	}
	int result = 0;
	d[0] = 0;
	while(true){
		int min_distance = 0x3f3f3f;
		int u;
		for(int j = 0;j < n;j++){ 
			if(d[j] < min_distance && (!visit[j])){
				min_distance = d[j];
				u = j;
			}
		}
//		cout << u << endl;
		if(min_distance == 0x3f3f3f)break;
		result += min_distance;
		visit[u] = true;
		d[u] = min_distance;
		for(int s = 0;s < n;s++){
			if(M[u][s] == -1)continue;
			if(M[u][s] < d[s]){
				d[s] = M[u][s];
				p[s] = u;
			}
		}
//		for(int s = 0;s < n;s++)cout << d[s] <<" ";
//		cout << endl;
	}
	cout << result << endl;
	
	
}