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

int main()
{
  int n;
  vector<int>list;
  int w=0;
  cin>>n;
  vector<vector<int> > A(n,vector<int>(n));
  for(int i=0;i<n;i++)
    {
      for(int j=0;j<n;j++)
        {
          cin>>A[i][j];
        }
    }
  list.push_back(0);
  int mini=2001;
  while(list.size()<n)
    {
      mini=2001;
      int k,l;
      for(int i=0;i<list.size();i++)
        {
          for(int j=0;j<n;j++)
            {
              if(A[list[i]][j]>=0)
                {
                  if(A[list[i]][j]<mini)
                    {
                      int f=1;
                      for(int s=0;s<list.size();s++)
                        {
                          if(j==list[s])
                            {
                              f=0;
                              break;
                            }
                        }
                      if(f)
                        {
                          mini=A[list[i]][j];
                          k=j;
                          l=list[i];
                        }
                    }
                }
            }
        }
      list.push_back(k);
      w+=mini;
    }
  cout<<w<<endl;
  return 0;
}

C++: #include<stdio.h>
using namespace std;
const int sss=1<<21;
int n;
int a[1000][1000];
int d[1000],p[10000],color[1000];
int prim(){
	int u,minv;
	for(int i=0;i<n;i++){
		d[i]=sss;
		p[i]=-1;
	}
	d[0]=0;
	while(1){
		minv=sss;
		u=-1;
		for(int i=0;i<n;i++){
			if(minv>d[i]&&color[i]!=1){
				u=i;
				minv=d[i];
			}
		}
		if(u==-1) break;
		color[u]=1;
		for(int v=0;v<n;v++){
			if(color[v]!=1&&a[u][v]!=sss){
				if(d[v]>a[u][v]){
					d[v]=a[u][v];
					p[v]=u;
				}
			}
		}
	}
	int sum=0;
	for(int i=0;i<n;i++){
		if(p[i]!=-1) {
			sum+=a[i][p[i]];
		}
	}
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int e;scanf("%d",&e);
			a[i][j]=e==-1?sss:e;
		}
	}
	printf("%d\n",prim());
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#define INF 99999
using namespace std;

int G[110][110] = {};

int prim(int n)
{
    int mini[110];
    int used[110];

    //init
    for(int i = 0;i < n;i++){
        mini[i] = INF;
        used[i] = 0;
    }
    mini[0] = 0;
    int ans = 0;

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

        used[v] = 1;
        ans += mini[v];
        //cout << ans << endl;

        for(int i = 0;i < n;i++){
            mini[i] = min(mini[i], G[v][i]);
        }
    }
    return ans;
}

int main()
{
    int n;
    cin >> n;

    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            int cost;
            cin >> cost;
            if(cost == -1) G[i][j] = INF;
            else G[i][j] = cost;
        }
    }

    cout << prim(n) << endl;
    return 0;
}

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


int P[10010]; // 0 から 10000 までの頂点を取り扱い可能
void init(int N) { // 初期化 はじめは全ての頂点はバラバラ
  for (int i=0; i<N; i++) P[i] = i;
}
int root (int a) { //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);
}


int main(){
  int n;
  cin >> n;
  tuple<int,int,int> cost[10010]; //Array for cost
  int count = 0;
  for (int i = 0; i < n; i ++){
    for (int j = 0; j < n; j++){
      int t;
      cin >> t;
      if (t >= 0){
      get<1>(cost[count]) = i;
      get<2>(cost[count]) = j;
      get<0>(cost[count]) = t;
      count++;
    }
    }
  }
  sort(cost,cost + n*n);//sortする

  init(n + 10);

  int ans = 0;
  int anscount = 0;//このcountがnになった時点で終了する
  int k = 0;
  while (anscount != n-1){
    if (is_same_set(get<1>(cost[k]), get<2>(cost[k]))) {
      k ++;
    }
    else{
      unite(get<1>(cost[k]), get<2>(cost[k]));
      ans += get<0>(cost[k]);
      k++;
      anscount++;
    }
  }


  cout << ans << endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
  static const int MAX=100;
  static const int INF=(1<<21);
  static const int WHI=0;
  static const int GRA=1;
  static const int BLA=2;
  int n,M[MAX][MAX];
  int prim(){
    int u,minv;
    int d[MAX],p[MAX],color[MAX];
    for(int i=0;i<n;i++){
      d[i]=INF;
      p[i]=-1;
      color[i]=WHI;
    }
    
    d[0]=0;
    while(1){
      minv=INF;
      u=-1;
      for(int i=0;i<n;i++){
	if(minv>d[i]&&color[i]!=BLA){
	  u=i;
	  minv=d[i];
	}
      }

      if(u==-1){
	break;
      }
      color[u]=BLA;

      for(int v=0;v<n;v++){
	if(color[v]!=BLA&&M[u][v]!=INF){
	  if(M[u][v]<d[v]){
	    d[v]=M[u][v];
	    p[v]=u;
	    color[v]=GRA;
	  }

	}
      }
    }

    int sum=0;
    for(int i=0;i<n;i++){
      if(p[i]!=-1){
	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;
	if(e==-1){
	  M[i][j]=INF;
	}else{
	  M[i][j]=e;
	}
      }
    }
    
    cout<<prim()<<endl;
    return 0;
  }

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

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

signed main() {
	int g;
	cin >> g;
	vector<pair<int, pair<int, int>>> a;
	int weight;

	init(g);

	for (int i = 0; i < g; ++i) {
		for (int j = 0; j < g; ++j) {
			cin >> weight;
			if (i > j and weight != -1) {
				a.push_back(make_pair(weight, make_pair(i, j)));
			}
		}
	}

	sort(a.begin(), a.end());

	int ans = 0;

	for (auto e : a) {
		//cout << e.first << ' ' << e.second.first << ' ' << e.second.second << endl;
		if (!is_same_set(e.second.first, e.second.second)) {
			unite(e.second.first, e.second.second);
			ans += e.first;
		}
		else {
			continue;
		}
	}
	cout << ans << endl;
}