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 <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>

using namespace std;

#define MAX_N 100

int G[MAX_N][MAX_N];

int mincost[MAX_N];
bool used[MAX_N];
int V;
int prim() {
	for (int i = 0; i < V; i++) {
		mincost[i] = INT_MAX;
		used[i] = false;
	}
	mincost[0] = 0;
	int res = 0;

	while (1) {
		int v = -1;
		for (int u = 0; u < V; u++)
			if (!used[u] && (v == -1 || mincost[u] < mincost[v]) )
				v = u;

		if (v == -1)
			break;
		used[v] = true;
		res += mincost[v];

		for (int u = 0; u < V; u++)
			mincost[u] = min(mincost[u], G[v][u]);
	}
	return res;
}

int main() {
	int n, c;
	cin >> n;
	V = n;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++) {
			cin >> c;
			if (c == -1) c = INT_MAX;
			G[i][j] = c;
		}

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

C++: #include<bits/stdc++.h>
#define REP(i,n) for(long long i=0;i<n;++i)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
using namespace std;
const int MAX=100,INF=1<<30,WHI=0,GRA=1,BLA=2;
int n,m[MAX][MAX];
int prim(){
    int u,minv;
    int d[MAX],p[MAX],color[MAX];
    fill(d,d+n+1,INF);
    fill(p,p+n+1,-1);
    fill(color,color+n+1,WHI);
    d[0]=0;
    while(1){
        minv=INF;
        u=-1;
        REP(i,n){
            if(minv>d[i]&&color[i]!=BLA){
                u=i;
                minv=d[i];
            }
        }
        if(u==-1) break;
        color[u]=BLA;
        REP(v,n){
            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;
    REP(i,n){
        if(p[i]!=-1) sum+=m[i][p[i]];
    }
    return sum;
}
int main(){
    scanf("%d",&n);
    REP(i,n){
        REP(j,n){
            int e;
            scanf("%d",&e);
            m[i][j]=(e==-1)?INF:e;
        }
    }
    printf("%d\n",prim());
    return 0;
}

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

typedef long long ll;


class Union_Find {
private:
    std::vector<int> par;
    std::vector<int> rank;

public:
    Union_Find(int n) { // ?????????: O(n)
	par = rank = std::vector<int>(n);
	for(int i=0; i<n; i++) {
	    par[i]  = i;
	    rank[0] = 0;
	}
    }

    int root(int x) { // ??¨??????????±???????: O(?±(n)) 
	if(par[x] == x) return x;
	return par[x] = root(par[x]); // ??????????????????????????????????????????
    }

    void unite(int x, int y) { // x,y????????????????????????: O(?±(n)) 
	x = root(x); y = root(y);
	if( x == y ) return;

	if( rank[x] < rank[y] ) par[x] = y;
	else {
	    par[y] = x;
	    if( rank[x] == rank[y] ) rank[x]++;
	}
    }

    bool same(int x, int y) { //x,y??????????????????????????????????????????: O(?±(n)) 
	return root(x) == root(y);
    }
};

struct Edge {
    ll s,t;
    ll w;
    bool operator < (const Edge& tgt) const {
	return w > tgt.w;
    }
};

ll Kruskal(priority_queue<Edge>& edges, const int V) {

    ll res = 0;
    
    Union_Find uf(V);
    while( !edges.empty() ) {
	Edge e = edges.top(); edges.pop();
	if( !uf.same(e.s, e.t) ) {
	    uf.unite(e.s, e.t);
	    res += e.w;
	}
    }
    return res;
}


int main () {

    ll V;
    cin >> V;

    priority_queue<Edge> edges;
    
    for(int i=0; i<V; i++) {
	for(int j=0; j<V; j++) {
	    ll d;
	    cin >> d;
	    if( d != -1 )
		edges.push( Edge{i,j,d} );
	}	
    }

    ll res = Kruskal(edges,V);
    cout << res << endl;    
    return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;

/*
  最小全域木

  重み付きグラフの最小全域木の辺の重みの総和を計算する．
  最小全域木とは？：グラフで，すべての頂点に到達できるものの中で，　辺の重みが最小のもの
  プリムのアルゴリズム

*/

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 u, minv;
  int d[MAX], p[MAX], color[MAX];
  //d: MSTに属する頂点とそれ以外の頂点の中で，重みが最小の辺の重み
  //p: p[v]  MSTにおける頂点vの親
  //color 訪問状態

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

  d[0] = 0;

  while( true ){
    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; 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;
        }
      }
    }
  }
  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;

  //入力
  REP(i,n)REP(j,n){
    int e;
    cin >> e;
    if(e == -1) M[i][j] = INFTY;
    else M[i][j] = e;
  }

  cout << prim() << endl;

  return 0;
}