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 <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#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 ALL(x) x.begin(),x.end()
#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 long long ll;
typedef pair<int,int> pii;
template<class A, class B>inline bool chmax(A &a, B b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, B b){return b<a ? a=b,1 : 0;}
inline bool inside(int y, int x, int H, int W){return(y>=0 && x>=0 && y<H && x<W);}
#define INF 0x3f3f3f3f
//_______

template <class T>
using Priority_que = priority_queue< T, vector<T>, greater<T> >;

int N;
vector<pii> G[102];
int mcos[102];

void dijkstra(int s)
{
   Priority_que < pii > pq;
   pq.push( mkp(0, s) );
   memset( mcos, INF, sizeof(mcos) );
   mcos[s] = 0;

   while(!pq.empty()){
      const pii& tmp = pq.top();
      pq.pop();
      const int u = tmp.se;
      if( mcos[u] < tmp.fi )continue;
      for( auto& v : G[u] ){
         const int nc = v.fi + tmp.fi;
         const int nxt = v.se;
         if( chmin( mcos[nxt], nc ) ){
            pq.push( mkp(nc, nxt) );
         }
      }
   }
   return ;
}

int slv()
{
   dijkstra(0);
   return ( *max_element(mcos, mcos+N) );
}

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

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

   outl( slv() );
}

C++: #include <iostream>
#include <vector>
#include <numeric>

using namespace std;

const int N = 100;

/*
// ????????????
Vnew = {x}???????????§ x ??? V ????????§??????????????????????????????????????????
Enew = {}
while (Vnew ??? V)
    Vnew ????????????????????? u ??¨ ????????????????????? v ????????¶?????????????°??????? (u, v) ??? E ????????????
    v ??? Vnew ????????????
    (u, v) ??? Enew ????????????
*/

enum Color : int {
    WHITE,
    GRAY,
    BLACK,
};

int main() {
    int A[N][N];

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int a;
            cin >> a;
            A[i][j] = a == -1 ? a : numeric_limits<int>::max();
        }
    }

    vector<int> color(n, WHITE);
    vector<int> d(n, numeric_limits<int>::max());
    vector<int> p(n, -1);

    d[0] = 0;
    while (1) {
        int minv = numeric_limits<int>::min();
        int 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 && A[u][v] != numeric_limits<int>::max()) {
                if (d[v] > A[u][v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum;
    for (int i =0; i < n; ++i) {
        if (p[i] != -1)
            sum += A[i][p[i]];
    }
    cout << sum << endl;

    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define INF (1 << 29)

const int MAX_V = 1000;

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int V;

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

	while (true) {
		int v = -1;
		// Xに属さない頂点のうちXからの辺のコストが最小になる頂点を探す
		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;     // 頂点vをXに追加
		res += mincost[v];  // 辺のコストを加える

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


int main() {
	cin >> V;

	for (int i = 0; i<V; i++) {
		for (int j = 0; j<V; j++) {
			int e; cin >> e;
			cost[i][j] = (e == -1) ? INF : e;
		}
	}

	cout << prim() << endl;
}

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


// ????????????union-find tree
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; // a???root
  return (P[a] = root(P[a])); // a?????????root????±????, a????????¨??????
}
bool is_same_set(int a, int b){ // a??¨b???????????°?????????????±?????????????
  return root(a) == root(b);
}
void unite(int a, int b){ // a??¨b???????????°?????????????????¨??????
  P[root(a)] = root(b);
}
// ???????????§???union-find tree


int main(){
  int n;
  cin >> n;
  pair<int,int> E[1000010];
  int a;
  int k=0;
  int m=0;
  int S[10010], G[10010];
  for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++){
      cin >> a;
      E[k].first = a; // ????????????
      E[k].second = m; // ????????????
      S[m] = i;
      G[m] = j;
      k++;
      m++;
      
    }
  }
    
#if 0
  for(int i=0; i<n*n; i++){
    cout << E[i].first << " " << E[i].second;
    cout << " -> " << S[E[i].second] << " " << G[E[i].second] << endl;
  }
#endif


  sort(E, E+n*n);

#if 0
  cout << endl;
  for(int i=0; i<n*n; i++){
    cout << E[i].first << " " << E[i].second;
    cout << " -> " << S[E[i].second] << " " << G[E[i].second] << endl;
  }
#endif
  
  
  int T = 0; // ?£?T????????????
  init(n); // ??¢??°??????, ?????°??????????????°

  for(int i=0; i<n*n; i++){
    if(E[i].first!=-1){
      if(is_same_set(S[E[i].second],G[E[i].second])==0){
	T += E[i].first;
	unite(S[E[i].second],G[E[i].second]);
      }
    }
    
  }

  cout << T << endl;
}