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 <bits/stdc++.h>
using namespace std;
#define INF (1<<30)
#define MAX_N 100
typedef pair<int, int> P;

struct edge{
    int v[2], cost;
    edge(){}
    edge(int v0, int u0, int cost0){
        v[0] = v0; v[1] = u0; cost = cost0;
    }
    bool operator < (const edge &e) const {
        return cost > e.cost;
    }
};

int ans = 0, N;
vector<edge> E[MAX_N];

void prim(){
    bool seen[N];
    fill(seen, seen + N, false);
    priority_queue<edge> todo;
    seen[0] = true;
    for(int i = 0; i < E[0].size(); i++)
        todo.push(E[0][i]);
    while(!todo.empty()){
        edge e = todo.top(); todo.pop();
        if(seen[e.v[1]]) continue;
        ans += e.cost;
        seen[e.v[1]] = true;
        for(int i = 0; i < E[e.v[1]].size(); i++)
            if(!seen[E[e.v[1]][i].v[1]]) todo.push(E[e.v[1]][i]);
    }
}

int main(){
    cin >> N;
    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++){
            int cost;
            cin >> cost;
            if(i < j && cost != -1){
                E[i].push_back(edge(i, j, cost));
                E[j].push_back(edge(j, i, cost));
            }
        }
    prim();
    cout << ans << endl;
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
//_____
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i,f,n) for(int i=(f); i<=(n); ++i)
#define err(...) fprintf(stderr, __VA_ARGS__)
#define show(x) cerr << #x << ':' << x << endl
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
typedef pair<int,int> pii;
#define INF 0x3f3f3f3f
//_______
vector<pii> G[102];
bool used[102];

int prim()
{
   priority_queue < pii, vector<pii>, greater<pii> > pq;
   pq.push( mkp(0,0) );
   int sum = 0;

   while( !pq.empty() ){
      const pii  tmp = pq.top(); pq.pop();
      if( used[ tmp.se ]++ )continue;
      sum += tmp.fi;

      rep(j, G[tmp.se].size() )
         pq.push( mkp( G[tmp.se][j].fi, G[tmp.se][j].se ) );
   }
   return (sum);
}

signed main()
{
   FAST();
   int N; cin >> N;

   rep(i, N)rep(j, N){
      int v; cin >> v;
      if( v != -1 ) G[i].pb( mkp(v,j) );
   }
   outl( prim() );
}

C++: #include<bits/stdc++.h>

std::vector<int> color;
std::vector<std::vector<int>> M;
std::vector<int> d;
std::vector<int> p;
int INF = 1001001001;
int sum = 0;

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

    d[0] = 0;
    p[0] = -1;

    while(true){
        int u;
        int mincost = INF;
        for(int i = 0; i < n; i++){
            if(color[i] != 1 && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }

        if(mincost == INF){
            break;
        }

        color[u] = 1;

        for(int i = 0; i < n; i++){
            if(color[i] != 1 && M[u][i] != -1){
                if(M[u][i] < d[i]){
                    d[i] = M[u][i];
                }
            }
        }

    }
    
    for(int i = 0; i < d.size(); i++){
        sum += d[i];
    }

    return sum;
}

int main(void){
    int n;
    std::cin >> n;

    color.resize(n);
    M.resize(n, std::vector<int>(n));
    d.resize(n);
    p.resize(n);

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            std:: cin >> M[i][j];
        }
    }
    
    
    

    std::cout << prim(n) << std::endl;

    return 0;
}

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

int n;
int a[101][101];

set<int>s,t;

int main(){
	s = set<int>();
	t = set<int>();
	scanf("%d",&n);
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			scanf("%d",&a[i][j]);
		}
		if(i == 0){
			t.insert(i);
		}
		else{
			s.insert(i);
		}
	}
	int ans = 0;
	while(!s.empty()){
		int min_s=-1,min_t=-1,min_w=10000000;
		for(set<int>::iterator sit = s.begin(); sit != s.end(); sit++){
			for(set<int>::iterator tit = t.begin(); tit != t.end(); tit++){
				if(a[*sit][*tit] != -1 && a[*sit][*tit] < min_w){
					min_s = *sit;
					min_t = *tit;
					min_w = a[*sit][*tit];
				}
			}
		}
		s.erase(min_s);
		t.insert(min_s);
		ans += min_w;
	}
	printf("%d\n",ans);
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef float fl;
typedef long long ll;

static const int MAX = 100;
static const int INF = (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];

    rep(i,n) {
        d[i] = INF;
        p[i] = -1;
        color[i] = WHITE;
    }
    d[0] = 0;

    while(1){
        minv = INF;
        u = -1;
        rep(i,n) {
            if( minv > d[i] && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1) break;
        color[u] = BLACK;
        rep(v,n) {
            if (color[v] != BLACK && M[u][v] != INF) {
                if ( d[v] > M[u][v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    rep(i,n) {
        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;
            M[i][j] = (e == -1)? INF : e;
        }
    } 
    cout << prim() << endl;
    return 0;
}