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 <algorithm>
#include <vector>
#include <utility>
#include <iostream>
using namespace std;
typedef pair<int, pair<int,int> > Edge;

int* uni;
void init(int N)
{
    uni = new int[N];
    for (int i = 0; i < N; i++)
    {
        uni[i] = -1;
    }
}

int root(int a)
{
    if (uni[a] < 0) return a;
    else return uni[a] = root(uni[a]);
}

bool connect(int a, int b)
{
    a = root(a);
    b = root(b);
    if (a == b) return false;
    if (uni[a] > uni[b]) swap( a, b);
    uni[a] += uni[b];
    uni[b] = a;
    return true;
}

int main(){
  int n;
  cin >> n;
  vector<Edge> graph(n);
  for (int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int tmp;
      cin >> tmp;
      if (tmp != -1){
         graph.emplace_back(tmp, make_pair(i,j));
      }
    }
  }
  sort(graph.begin(), graph.end());
  init(n);
  int sum=0;
  for (auto ite=graph.begin();ite != graph.end();ite++){
    if (connect(ite->second.first, ite->second.second))
        sum+=ite->first;
  }
  cout << sum << endl;
}

C++: #include <stdio.h>
#define MAX 100
static const int INFTY = (1<<21);
int n,A[MAX][MAX];

int prim();

int main(){
  int i,j,s;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&s);
      A[i][j]=(s==-1)?INFTY : s;
    }
  }
  printf("%d\n",prim());
  return 0;
}

int prim(){
  int e,min,i,v,sum=0;
  int g[MAX],r[MAX],a[MAX];

  for(i=0;i<n;i++){
    g[i]=INFTY;
    r[i]=-1;
    a[i]=0;
  }
  g[0]=0;

  while(1){
    min=INFTY;
    e=-1;
    for(i=0;i<n;i++){
      if(min>g[i]&&a[i]!=2){
	e=i;
	min=g[i];
      }
    }
    if(e==-1)
      break;
    a[e]=2;
    for(v=0;v<n;v++){
      if(a[v]!=2&&A[e][v]!=INFTY){
	if(g[v]>A[e][v]){
	  g[v]=A[e][v];
	  r[v]=e;
	  a[v]=1;
	}
      }
    }
  }
  for(i=0;i<n;i++){
    if(r[i]!=-1)
      sum+=A[i][r[i]];
  }
  return sum;
}

C++: #include <iostream>
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int MAX = 2000;

int d[110], p[110];
int color[110];

int main() {
  int n;
  std::cin >> n;
  int G[110][110];

  for(int i=0; i<n; ++i) {
    for(int j=0; j<n; ++j) {
      int v;
      std::cin >> v;
      if(v != -1) {
        G[i][j] = v;
      } else {
        G[i][j] = MAX;
      }
    }
  }

  for(int i=0; i<n; ++i) {
    color[i] = WHITE;
    d[i] = MAX;
  }

  d[0] = 0;
  p[0] = 0;

  while(true) {
    int mini_cost = MAX;
    int u;
    // 最小の重みとなる辺を探している
    for(int i=0; i<n; ++i) {
      if(color[i] != BLACK && d[i] < mini_cost) {
        mini_cost = d[i];
        u = i;
      }
    }

    if(mini_cost == MAX) {
      break;
    }

    color[u] = BLACK;

    for(int v=0; v < n; ++v) {
      if(color[v] != BLACK && G[u][v] != MAX) {
        if(G[u][v] < d[v]) {
          d[v] = G[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int d_sum = 0;
  for(int i=0; i<n; ++i) {
    d_sum += d[i];
  } 

  std::cout << d_sum << std::endl;
}

C++: #include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=111;
const int inf=0x3f3f3f3f;
int e[maxn][maxn];
int n;
int  p[maxn];
int b[maxn];
int vis[maxn];
void prime()
{
    memset(vis,0,sizeof(vis));
    memset(p,-1,sizeof(p));
    for(int i=0; i<n; i++)
        b[i]=inf;
    b[0]=0;
    while(1)
    {
        int ans=inf;
        int v=-1;
        for(int i=0; i<n; i++)
        {
            if(vis[i]==0&&b[i]<ans)
            {
                v=i;
                ans=b[i];
            }
        }
        if(v==-1)
            break;
        vis[v]=1;
        for(int i=0; i<n; i++)
        {
            if(b[i]>e[v][i]&&vis[i]==0)
            {
                b[i]=e[v][i];
                p[i]=v;
                //printf("%d\n",v);
            }
        }
    }
 
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(p[i]!=-1)
            sum+=e[p[i]][i];
    }
    printf("%d\n",sum);
}
int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
        {
            int x;
            scanf("%d",&x);
            if(x==-1)
                e[i][j]=inf;
            else e[i][j]=x;
        }
    prime();
    return 0;
}

C++: #include<iostream>

using namespace std;

static const int MAX = 100;
static const int NIL = (1 << 21);
static const int WHITE = 0;
static const int GREY = 1;
static const int BLACK = 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] = INFY;
    p[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; i < n; i++) {
      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;
        }
      }
    }
  }
  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;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}