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>
#include<vector>
using namespace std;

const int INF = 1 << 30;

struct Vertex {
	int id;
	int w;
	vector<Vertex> vtxs;
};

int prim(int n, Vertex vtxs[]){
	
	int d[n];
	bool vst[n];
	for(int i=0;i<n;i++){
		d[i] = INF;
		vst[i] = false;
	}
	
	d[0]=0;
	while(true) {
		int minv = INF;
		int u = -1;
		for(int i=0;i<n;i++){
			if(vst[i]) continue;
			if(minv > d[i]){
				minv = d[i];
				u = i;
			}
		}
		if(u==-1) break;
		vst[u]=true;
		vector<Vertex> vec=vtxs[u].vtxs;
		for(int v=0;v<vec.size();v++){
			Vertex vtx=vec[v];
			int id=vtx.id;
			if(vst[id]) continue;
			d[id]=min(d[id],vtx.w);
		}
	}
	int cost=0;
	for(int i=0;i<n;i++){
		cost+=d[i];
	}
	return cost;
}

int main(){
	int n;
	cin >> n;
	Vertex vtxs[n];
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int w;
			cin >> w;
			if(w==-1) continue;
			Vertex v;
			v.id = j;
			v.w = w;
			vtxs[i].vtxs.push_back(v);
		}
	}
	int cost = prim(n,vtxs);
	cout << cost << endl;
	return 0;
}

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;
const int MAX=100;
const int INF=(1<<21);
const int W=0;
const int G=1;
const int B=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]=INF;
    p[i]=-1;
    color[i]=W;
  }
  d[0]=0;
  while(1){
    minv=INF;
    u=-1;
    for(int i=0;i<n;i++){
      if(minv>d[i] && color[i]!=B){
	u=i;
	minv=d[i];
      }
    }
    if(u==-1) break;
    color[u]=B;
    for(int v=0;v<n;v++){
      if(color[v]!=B && m[u][v]!=INF){
	if(d[v]>m[u][v]){
	  d[v]=m[u][v];
	  p[v]=u;
	  color[v]=G;
	}
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    if(p[i]!=-1) sum+=m[i][p[i]];
  }
  return sum;
}

int main(){
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int e;
      scanf("%d",&e);
      m[i][j]=(e==-1)?INF:e;
    }
  }
  printf("%d\n",prim());
  return 0;
}

C++: #include <iostream>
using namespace std;
#define INF 10000000

int main(){
  long n;
  long G[100][100],d[100];
  long minc,mi,i;
  bool flg[100];
  long sum=0;
  
  cin >> n;
  for (i=0;i<n;i++){
    for (int j=0;j<n;j++){
      cin >> G[i][j];
      if (G[i][j]==-1) G[i][j]=INF;
    }
    d[i]=INF;
    flg[i]=false;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (!flg[i]&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flg[mi]=true;
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (!flg[i]&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    sum+=d[i];
  }
  cout << sum<<endl;
  
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
const int num = 10000;
const int INF = 2000000000;
int c[num];
int pe[num];
int d[num];
typedef pair<int, int> p;
vector <p> v[num];
int main() {
	int n;
	cin >> n;
	fill(pe, pe + n, INF);
	fill(c, c + n, 0);
	fill(d, d + n, INF);
	d[0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int w; cin >> w;
			if (w != -1) v[i].push_back(p(w, j));
		}
	}
	priority_queue <p, vector<p>, greater<p> > q;
	for (int i = 0; i < v[0].size(); i++) {
		q.push(p(v[0][i].first, v[0][i].second));
		d[v[0][i].second] = v[0][i].first;
		pe[v[0][i].second] = 0;
	}
	c[0] = 1;
	while (!q.empty()) {
		int f = q.top().first;
		int s = q.top().second;
		q.pop();
		c[s] = 1;
		if (d[s] < f) continue;
		for (int i = 0; i < v[s].size(); i++) {
			int fi = v[s][i].first;
			int se = v[s][i].second;
			if (c[se]) continue;
			if (pe[se] == INF || d[se] > fi) {
				d[se] = fi;
				pe[se] = s;
				q.push(p(fi, se));
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++)
		sum += d[i];
	cout << sum << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n + 1); ++i)
using namespace std;
using ll = long long;
const int INF = +100100100;
// typedef pair<int,int> P;
const int  WHITE = 0;
const int GRAY =1;
const int BLACK =2;
const int NIL = -1;
const int MAX = 100;

// 配列のサイズを大きく取りすぎるとエラーが出ないが、実行できなくなる.
vector<vector<int>> M(MAX,vector<int>(MAX,INF));
vector<int> color(MAX,WHITE), P(MAX,NIL),D(MAX,INF);
int n;


int prim(){
    int s = 0;
    int u;
    D[0]=0;
    while(1){
        int d_min = INF;
        // 線の内、始点を選ぶ
        rep(i,n){
            if(color[i]!=BLACK && D[i]<d_min){
                d_min = D[i];
                u=i;
            }
        }
        
        if(d_min==INF)break;
        color[u]=BLACK;
        // 終点を選ぶかつ、選び終えたら、Dへ最小値を保存
        rep(v,n){
            if(M[u][v]!=INF && color[v]!=BLACK){
                if(M[u][v]<D[v]){
                    D[v]=M[u][v];
                    P[v]=u;
                    color[v]=GRAY;
                    
                }
            }
        }        
    }
    int sum=0;
    rep(i,n){
        if(P[i]!=NIL)sum+=M[i][P[i]];
    }
    return sum;
}

int main(){
    cin >> n; 
    rep(i,n){
        
        rep(j,n){
          int x;
          cin >> x;
          if(x==-1)x=INF;
          M[i][j]=x;  
          
        }
    }
    printf("%d\n",prim());
    return 0;
}