Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

C++: #include <stdio.h>

#define N 2000
#define BLACK 0
#define WHITE 1
#define GRAY 2
#define inf 200000

int Map[N][N];
int n;
int color[N];
int d[N];
int p[N];


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

void prim(){
	init();
	int mincost;
	d[0] = 0;
	p[0] = -1;
	int i;
	int u;
	int v;
	while(1){
		mincost = inf;
		for(i=0;i<n;i++){
			if(color[i]!=BLACK && d[i] < mincost){
				mincost = d[i];
				u = i;
			}
		}

		if(mincost == inf){
			break;
		}
		color[u]=BLACK;
		for(v=0;v<n;v++){
			if(color[v] != BLACK && Map[u][v]!= -1){
				if(Map[u][v] < d[v]){
					d[v] = Map[u][v];
					color[v] = GRAY;
					p[v] = u;
				}
			}
		}
	}
}

int main(){
	scanf("%d",&n);
	int i,j;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			scanf("%d",&Map[i][j]);
		}
	}
	prim();
	int total = 0;
	for(i=0;i<n;i++){
		total += d[i];
	}
	printf("%d\n",total);
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <functional>
#include <vector>
using namespace std;

#define V vector<vector<pair<int,int>>>

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	int n,Ans = 0; cin >> n;
	V v(2001);
	vector<int> f(n + 1,0);
	for (int i = 1; i <= n;i++) {
		for (int j = 1; j <= n; j++) {
			int a; cin >> a;
			if (a != -1) {
				v[a].push_back(make_pair(i, j));
			}
		}
	}

	for (int i = 0; i <= 2000;i++) {
		for (int j = 0; j < v[i].size(); j++) {
			int first = v[i][j].first, second = v[i][j].second;
			if (f[first] >= 2 || f[second] >= 2) continue;
			Ans += i;
			f[first]++; f[second]++;
		}
	}

	cout << Ans << endl;
}

C++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ll = long long;
using Pi_i = pair<int, int>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;

template<class T, class S>
bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S>
bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}

/*　O(V log E) 
#include <algorithm>
#include <queue>
#include <vector>
#define FOR(i, s, n) for(int i = s, i##_len=(n); i < i##_len; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
const int INF = 1000000000;
using namespace std;
using ll = long long;
using Pi_i = pair<int, int>;
*/

struct edge{
    int to, cost;
    edge(int To, int Cost): to(To), cost(Cost){};
};


int V; //頂点数
vector<vector<edge>> G;
vector<int> d; //既に木に含まれる点からの距離

int prim(){
    int res = 0;
    //Pi_i first: 距離　second: 頂点
    priority_queue<Pi_i, vector<Pi_i>, greater<Pi_i>> que;
    fill(ALL(d), INF);
    d[0] = 0;
    que.push(MP(0, 0));

    while(!que.empty()){
        Pi_i p = que.top(); que.pop();
        int v = p.second;
        if(d[v] < p.first) continue;
        d[v] = 0;
        res += p.first;
        for(edge &e : G[v]){
            if(d[e.to] > e.cost){
                d[e.to] = e.cost;
                que.push(MP(d[e.to], e.to));
            }
        }
    }
    return res;
}

int main(){
    cin >> V;
    G.resize(V);
    d.resize(V);
    REP(i, V){
        REP(j, V){
            int cst; cin >> cst;
            if(cst != -1){
                G[i].PB(edge(j, cst));
                G[j].PB(edge(i, cst));
            }
        }
    }
    cout << prim() << endl;
    return 0;
}