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 <iostream>
#include <cstdio>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pa; 
#define mp make_pair;


int N,M,H,W,K,A,B;
string S;
string alpha = "abcdefghijklmnopqrstuvwxyz";
string ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  cin>>N;
  mat a(N,vec(N));
  rep(i,N) rep(j,N) cin>>a.at(i).at(j);
  class union_find{
        vector<long> parent, tree_size;
    public:
        union_find(unsigned long _n) : parent(_n), tree_size(_n, 1){
            for(unsigned long i = 0; i < _n; ++i)parent[i] = i;
        }
        ll find(ll x){
            while(parent[x] != x)x = parent[x] = parent[parent[x]];
            return x;
        }
        void merge(ll a, ll b){
            a = find(a);
            b = find(b);
            if(tree_size[a] < tree_size[b])swap(a, b);
            tree_size[a] += tree_size[b];
            parent[b] = a;
            return;
        }
	bool same(ll a, ll b){
	    a = find(a);
	    b = find(b);
	    return a == b;
	}
  };
  union_find uf(N);
  vector<pair<ll,ll> > edge(0);
  rep(i,N){
    reps(j,i+1,N){
      if(a[i][j] != -1){
	edge.push_back(pa(a[i][j],i*N+j));
      }
    }
  }
  sort(edge.begin(),edge.end());
  ll anse=0,ans=0;
  rep(i,edge.size()){
    pa e = edge[i];
    if(uf.same(e.se/N,e.se%N)) continue;
    ans += e.fi;
    uf.merge(e.se/N,e.se%N);
    ++anse;
    if(anse == N-1) break;
  }
  if(anse<N-1) cout<<"impossible"<<endl;
  cout<<ans<<endl;
  //uf.mergeのように使う
  //ufはMAX_N+1でとるか、代入時に0-indexedにする
}

C++: #include <iostream>
using namespace std;
#define MAX 100
#define INFTY (1<<21)
#define WHITE 0
#define GRAY 1
#define 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] = INFTY;
        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; 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;

    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            int e;
            cin >> e;
            if(e==-1) {
                M[i][j] = INFTY;
            }
            else {
                M[i][j] = e;
            }
        }
    }

    cout << prim() << endl;

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int INF   = (1 << 21);
static const int WHITE = 0;
static const int GRAY  = 1;
static const int BLACK = 2;
static const int NIL   = -1;

int n;

void prim(vector<vector<int>> &A) {
  vector<int> color(n, WHITE);
  vector<int> d(n, INF);
  vector<int> p(n, 0);
  d[0] = 0;
  p[0] = -1;

  while (true) {
    int mincost = INF;
    int u       = -1;

    for (int 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 (int v = 0; v < n; v++) {
      if (color[v] != BLACK && A[u][v] != -1) {
        if (A[u][v] < d[v]) {
          d[v]     = A[u][v];
          p[v]     = u;
          color[v] = GRAY;
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    res += d[i];
  }
  cout << res << endl;
}

int main() {
  cin >> n;
  vector<vector<int>> A(n, vector<int>(n));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> A[i][j];
    }
  }
  prim(A);
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

static const long long FAR = (1LL << 21);
static const int N = 100;

int n, m[N][N];
bool visited[N];
int d[N], p[N];

int prim(){
	for (int i = 0; i < n; ++i) {
		d[i] = FAR;
		p[i] = -1;
		visited[i] = false;
	}

	d[0] = 0;
	while (true)
	{
		int mi = FAR;
		int u = -1;
		for (int i = 0; i < n; ++i)	{
			if (!visited[i] && d[i] < mi){
				u = i;
				mi = d[i];
			}
		}

		if (-1 == u) break;
		visited[u] = true;

		for (int v = 0; v < n; ++v) {
			if (!visited[v] && m[u][v] < d[v]){
				p[v] = u;
				d[v] = m[u][v];
			}
		}
	}

	int res = 0;
	for (int i = 0; i < n; ++i) {
		if (p[i] != -1) res += m[i][p[i]];
	}
	return res;
}

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) ? FAR : e);
		}
	}

	cout << prim() << endl;

	return 0;
}

C++: #include <iostream>
#include <tuple>
#include <vector>
#include <algorithm>

int P[10000];

void init(int N)
{
    for (int i = 0; i < N; i++)
    {
        P[i] = i;
    }
}

int root(int a)
{
    if (P[a] == a)
    {
        return a;
    }
    else
    {
        return (P[a] = root(P[a]));
    }
}

bool is_same_set(int a, int b)
{
    return root(a) == root(b);
}

void unite(int a, int b)
{
    P[root(a)] = root(b);
}

int main()
{
    int n, weight = 0;
    std::cin >> n;
    init(n);
    std::vector<std::tuple<int, int, int>> edge;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int a;
            std::cin >> a;
            if (j >= i + 1 && a != -1)
            {
                edge.push_back(std::make_tuple(a, i + 1, j + 1));
            }
        }
    }
    sort(edge.begin(), edge.end());
    for (int i = 0; i < edge.size(); i++)
    {
        //        std::cout << std::get<0>(edge.at(i)) << " " << std::get<1>(edge.at(i)) << " " << std::get<2>(edge.at(i)) << std::endl;
        if (is_same_set(std::get<1>(edge.at(i)), std::get<2>(edge.at(i))) == 0)
        {
            weight += std::get<0>(edge.at(i));
            unite(std::get<1>(edge.at(i)), std::get<2>(edge.at(i)));
        }
    }
    std::cout << weight << std::endl;
    return 0;
}