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<iostream>
using namespace std;
static const int MAX =100;
static const int INFTY=(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];

  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;
      M[i][j]=(e==-1)?INFTY:e;
    }
  }
  cout<<prim()<<endl;
  return 0;
  
}

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;
}