Go: package main

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

const R = 100000000

var sc = bufio.NewScanner(os.Stdin)

var n int
var A [101][101]int

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func prim() int {
	var u, Minimum, sum int
	var d, p, color [101]int

	for i := 0; i < n; i++ {
		d[i] = R
		p[i] = -1
		color[i] = 0
	}
	d[0] = 0

	for {
		Minimum = R
		u = -1
		for i := 0; i < n; i++ {
			if Minimum > d[i] && color[i] != 2 {
				u = i
				Minimum = d[i]
			}
		}
		if u == -1 {
			break
		}
		color[u] = 2
		for j := 0; j < n; j++ {
			if color[j] != 2 && A[u][j] != R {
				if d[j] > A[u][j] {
					d[j] = A[u][j]
					p[j] = u
					color[j] = 1
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += A[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++ {
			e := nextInt()
			if e == -1 {
				A[i][j] = R
			} else {
				A[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

C++: //専用ライブラリ
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll>P;
#define INF 10e16
#define MOD 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define fi first
#define se second
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define PI acos(-1.0)
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int cmp(P a,P b){
        if(a.fi!=b.fi)
        return a.fi>b.fi;
        else
        return a.se<b.se;
}
//---------------------------------------------------------------------------
#define INFTY 2001
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 100

// 最小全域木を構成し、重みの総和を返す関数
int prim(int n, int A[N][N]) {
  int i, vertex, min_cost, sum_weight, cost[N], color[N];
  // 初期化
  for (i = 0; i < n; i++) {
    cost[i] = INFTY;
    color[i] = WHITE;
  }
  cost[0] = 0;
  color[0] = GRAY;
  sum_weight = 0;
  
  while (1) {
    // Tに隣接する頂点の中で最小のコストを持つものを選ぶ
    vertex = -1;
    min_cost = INFTY;
    for (i = 0; i < n; i++) {
      if (color[i] == GRAY && cost[i] < min_cost) {
        min_cost = cost[i];
        vertex = i;
      }
    }
    // T = V ならば終了
    if (vertex == -1) break;

    color[vertex] = BLACK;  // vertexをTに加える
    sum_weight += cost[vertex];

    // vertexに隣接し、Tに属していない頂点のコストを更新
    for (i = 0; i < n; i++) {
      if (color[i] != BLACK && A[vertex][i] < INFTY) {
        color[i] = GRAY;
        if (cost[i] > A[vertex][i]) cost[i] = A[vertex][i];
      }
    }
  }

  return sum_weight;
}

/*
// 最小全域木の辺の重みの和を計算する関数
int sum_weight(int n, int parent[], int A[N][N]) {
  int sum = 0;
  
  for (int i = 1; i < n; i++) sum += A[i][parent[i]];
  
  return sum;
}*/

//---------------------------------------------------------------------------
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    int i, j, n, w, A[N][N];
    cin >> n;
    // 隣接行列が与えられる
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        cin >> w;
        if (w == -1) A[i][j] = INFTY;
        else A[i][j] = w;
      }
    }

    cout << prim(n, A) << endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool f[100];
int n;
int tr[100];


int trmin(){
  int res=10000,resi;
  for(int i=0;i<n;i++)
    if(f[i]==0 && tr[i]<res) res = tr[i],resi=i;
  return resi;
}


int main (){

  int mp[100][100];

  cin >> n;
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      cin >> mp[i][j];

  for(int i=0;i<n;i++) f[i]=0;
  for(int i=0;i<n;i++) tr[i]=10000;
  tr[0] = 0;
  int trsum=0;

  for(int k=0;k<n;k++){
    int r=trmin();
    f[r]=1;
    trsum+=tr[r];

    for(int i=0;i<n;i++) 
      if(mp[r][i]!=-1)
	tr[i] = min(tr[i],mp[r][i]);
  }
  cout<<trsum<<endl;

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF 2*1e9
int main(){
  
  int n;
  int cost[100][100];
  int mincost[100];
  int used[100]={};
  
  cin>>n;
  for(int i=0;i<n;i++){
    mincost[i]=INF;
    for(int j=0;j<n;j++){
      cin>>cost[i][j];
      if(cost[i][j]==-1)cost[i][j]=INF;
    }
  }

  mincost[0]=0;
  int ans=0;

  while(1){
    int v=-1;
    for(int u=0;u<n;u++){
      if(used[u]==0&&(v==-1||mincost[u]<mincost[v]))v=u;
    }
    
    if(v==-1)break;
    used[v]=1;
    ans+=mincost[v];
    
    for(int u=0;u<n;u++){
      mincost[u]=min(mincost[u],cost[v][u]);
    }
  }
  
  cout<<ans<<endl;
  
  return 0;
  
}

C++: #include<iostream>
using namespace std;
#define MAX 1000
#define Infty 10000

struct Edge{int cost, left, right; };

void Bubble(struct Edge A[], int N){
  for(int i=0;i<N;i++){
    for(int j=N-1;j>=i;j--){
      if(A[j].cost < A[j-1].cost){
        Edge buffa = A[j];
        A[j] = A[j-1];
        A[j-1] = buffa;
      }
    }
  }
}
void print(struct Edge A[], int N){
  for(int i=0;i<N;i++){
    cout << A[i].cost << A[i].left << A[i].right << endl;
  }
}

int find(int x, int parents[]){
  int c = x;
  while(parents[c]!=c) c = parents[c];
  return c;
}

void uunion(int x, int y, int parents[]){
  int a = find(x,parents);
  int b = find(y,parents);
  if(a!=b) parents[a] = b;
}

int main(){
    int n, M[MAX][MAX], d=0;
    int k = 0;
    struct Edge A[MAX];
    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;
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        if(j<i){
          //cout << "k = " << k << endl;
          A[k].cost = M[i][j];
          //cout << "M[1][1]=" << M[i][j] << endl;
          //cout << "A.cost = " << A[k].cost << endl;
          A[k].right = i;
          A[k].left = j;
          k++;
        }
      }
    }
    Bubble(A, k);
    
    int parents[k];
    for(int i=0;i<k;i++) parents[i] = i;
    for(int i=0;i<k;i++){
      if(find(A[i].left,parents)!=find(A[i].right,parents)){
        d += A[i].cost;
        uunion(A[i].left,A[i].right,parents);
      }
    }
    cout << d << endl;

    return 0;
}