Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

C++: #include <algorithm>
#include <iostream>
using namespace std;

int P[10010];
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 {
    P[a] = root(P[a]);
    return 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;
  cin >> N;
  pair <int, int> edge[10010];
  for (int i=0; i<N; i++){
    for (int j=0; j<N; j++){
      cin >> edge[i*N+j].first;
      edge[i*N+j].second = i*N+j;
    }
  }
  sort(edge, edge+N*N);
  init(N);
  int ans = 0;
  int b = 0;
  for (int k=0; k<N*N; k++){
    while(edge[k].first < 0) k++;
    int i = edge[k].second / N;
    int j = edge[k].second % N;
    if (b == 0){ //グラフは非連結かもしれない?
      b = i;
      unite(i,j);
      ans += edge[k].first;
    }
    if (is_same_set(b,i) && is_same_set(b,j)){}
    else{
      unite(b,i);
      unite(b,j);
      ans += edge[k].first;
    }
  }
  cout << ans << endl;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <set>
#include <queue>
#include <list>
using namespace std;
using pii  = pair<int,int>;
using ll=long long;
using ld=long double;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
#define REP(i,a,b) for(int i=(a);i<(b);++i)
#define crep(i) for(char i='a';i<='z';++i)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define ALL(x) (x).begin(),(x).end()
#define endl '\n'
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
long long gcd(long long a, long long b){return (b == 0 ? a : gcd(b, a%b));}
long long lcm(long long a, long long b){return a*b/gcd(a,b);}
constexpr ll MOD=1000000007;
constexpr ld EPS=10e-8;
template<typename T> istream& operator>>(istream& is,vector<T>& v){for(auto&& x:v)is >> x;return is;}
template<typename T,typename U> istream& operator>>(istream& is, pair<T,U>& p){ is >> p.first; is >> p.second; return is;}
template<typename T,typename U> ostream& operator>>(ostream& os, const pair<T,U>& p){ os << p.first << ' ' << p.second; return os;}
template<class T> ostream& operator<<(ostream& os, vector<T>& v){
    for(auto i=begin(v); i != end(v); ++i){
        if(i !=begin(v)) os << ' ';
        os << *i;
    }
    return os;
}

vector<pair<ll,pair<ll,ll>>> V;
ll length=0,N;
ll chek[107];
ll A[107][107];

void dfs(ll pt,ll c){
    chek[pt]=1;
    if(c==N){
        return;
    }
    rep(i,N){
        if(pt!=i){
            V.pb(mp(A[pt][i],mp(pt,i)));
        }
    }
    sort(ALL(V));
    rep(i,V.size()){
        if(chek[V[i].second.second]==0){
            length+=V[i].first;
            dfs(V[i].second.second,c+1);
            break;
        }
    }
}
 
int main(){
    cin>>N;
    rep(i,N){
        rep(j,N){
            cin >> A[i][j];
            if(A[i][j]==-1){
                A[i][j]=MOD;
            }
        }
    }
    rep(i,N){
        if(0!=i){
            V.pb(mp(A[0][i],mp(0,i)));
        }
    }
    dfs(0LL,0LL);
    cout  << length << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
constexpr int INF = 1<<21;
enum Color { WHITE = 0, GRAY, BLACK };
int n;
vv(int, M, MAX, MAX, INF);
inline int prim() {
	vi d(MAX, INF), t(MAX, -1), color(MAX, WHITE);
	d[0] = 0;
	int minv, u;
	while(true) {
		minv = INF;
		u = -1;
		rep(i, 0, n) {
			if(minv > d[i] && color[i] != BLACK) {
				u = i;
				minv = d[i];
			}
		}
		if(u == -1) break;
		color[u] = BLACK;
		rep(v, 0, n) {
			if(color[v] != BLACK && M[u][v] != INF && d[v] > M[u][v]) {
				d[v] = M[u][v];
				t[v] = u;
				color[v] = GRAY;
			}
		}
	}
	int sum = 0;
	rep(i, 0, n) {
		if(t[i] != -1) sum += M[i][t[i]];
	}
	return sum;
}
int main() {
	cin >> n;
	int e;
	rep(i, 0, n) {
		rep(j, 0, n) {
			cin >> e;
			if(e != -1) M[i][j] = e;
		}
	}
	P(prim());
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,M[105][105];
int prim(){
  int u,minv;
  int d[105],p[105],color[105];
  for(int i=0;i<n;i++){
    d[i]=1234567890;
    p[i]=-1;
    color[i] = 0;
  }
  d[0]=0;
  while(1){
    minv = 1234567890;
    u = -1;
    for(int i=0;i<n;i++){
      if(minv > d[i] && color[i]!=2){
	u=i;
	minv = d[i];
      }
    }
    if(u==-1)break;
    color[u]=2;
    for(int v=0;v<n;v++){
      if(color[v]!=2&&M[u][v]!=1234567890){
	if(d[v]>M[u][v]){
	  d[v]=M[u][v];
	  p[v]=u;
	  color[v]=1;
	}
      }
    }
  }
  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;
      M[i][j]=(e==-1)? 1234567890 : e;
    }
  }
  cout << prim() << endl;
  return 0;
}

C++: #include <stdio.h>

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
const int N = 100;
const int INF = 2000000000;

int g[N + 1][N + 1];
int d[N + 1], p[N + 1], color[N + 1];
int n;

void prim()
{
	for (int i = 1; i <= n; i++)
	{
		color[i] = WHITE;
		d[i] = INF;
		p[i] = -1;
	}
	d[1] = 0;

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

		color[u] = BLACK;

		for (int i = 1; i <= n; i++)
		{
			if (color[i] != BLACK && g[u][i] != INF && d[i] > g[u][i])
			{
				d[i] = g[u][i];
				color[i] = GRAY;
				p[i] = u;
			}
		}
	}
	int sum = 0;
	for (int i = 1; i <= n; i++)
	{
		if (p[i] != -1)
			sum += g[p[i]][i];
	}
	printf("%d\n", sum);
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			scanf("%d", &g[i][j]);
			if (g[i][j] == -1)
				g[i][j] = INF;
		}
	prim();

	return 0;
}