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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0; i<(n); i++)
#define REP2(i,x,n) for(ll i=x; i<(n); i++)
#define REP3(i,x,n) for(ll i=x; i>(n); i--)
#define ALL(n) begin(n),end(n)

#define SENTINEL 2000000000
#define NIL -1
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
const long long INF = numeric_limits<long long>::max();
static const int MAX = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int INFTY = (1<<21);

int n,M[MAX][MAX];

//primのアルゴリズム
int prim(){
    int color[MAX],d[MAX],p[MAX];
    int minv;
    for (int u = 0; u < n; u++)
    {
        color[u] = WHITE;
        d[u] = INFTY;
        p[u] = -1;
    }
    d[0]=0; 
    while(1){
        minv = INFTY;
        int u= -1;
        for (int i = 0; i < n; i++)
        {
            if(color[i]!=BLACK&&d[i]<minv){
                minv = d[i];
                u = i;
            }
        }
        if (u == -1)break;
        color[u] = BLACK;
        for (int v = 0; v < n; v++)
        {
            if(color[v]!=BLACK&&M[u][v]!=INFTY){
                if(M[u][v]<d[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;
    REP(i,n){REP(j,n){
        int e;
        cin >> e;
        M[i][j] = (e ==-1)?INFTY:e;    
        }
    }
    cout << prim() << endl;

    return 0;
}

C++: #include <iostream>
using namespace std;
 
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1000000
 
 
int n,M[100][100],color[100],d[100],p[100];
 
//プリム法
int prim(){
  int i,u;

  //初期化
  for( i = 0; i < 100; i++ ){ 
    color[i] = WHITE;
    d[i] = INFTY;
    p[i] = -1;   
  }
  
  d[0] = 0;
  
  
  while(1){
    u = -1;
    int mincost = INFTY;
     
    for( i = 0; i < n; i++ ){
      if( color[i] != BLACK && d[i] < mincost ){
    mincost = d[i];
    u = i;
      }
    }
    
    if( u == -1 ) break;
 
    color[u] = BLACK;
 
    //最小全域木の更新
    for( int v = 0; v < n; v++ ){
      if( color[v] != BLACK && M[u][v] != INFTY ){
    if( M[u][v] < d[v] ){
      d[v] = M[u][v];
      p[v] = u;
      color[v] = GRAY;
    }
      }
    }
  }
  //最小全域木の辺の重みの総和をもとめる
  int sum = 0;
  for( 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++ ){
      cin >> M[i][j];
      if( M[i][j] == -1 ) M[i][j] = INFTY;
    }
  }
 
  cout << prim() << endl;
 
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

vector<vector<int>> v;
vector<int> d, color;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n, ans;

void prim(){
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
  q.push(make_pair(0, 0));
  d[0] = 0;

  while(!q.empty()){
    auto next = q.top(); q.pop();
    if(color[next.second] == BLACK) continue;

    color[next.second] = BLACK;
    ans += d[next.second];

    for(int i=0; i<n; ++i){
      if(i != next.second && v[next.second][i] != -1 && color[i] != BLACK && d[i] > v[next.second][i]){
        q.push(make_pair(v[next.second][i], i));
        d[i] = v[next.second][i];
        color[i] = GRAY;
      }
    }
  }
}

int main(){
  cin >> n;

  v.resize(n);
  d.resize(n);
  color.resize(n);

  for(int i=0; i<n; ++i){
    d[i] = numeric_limits<int>::max();
    v[i].resize(n);
    for(int l=0; l<n; ++l) cin >> v[i][l];
  }

  prim();

  cout << ans << endl;
}

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

using namespace std;

const int N = 100;



enum {
    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 <iostream>
#include <queue>
using namespace std;
#define MAX 105

int n, choice[MAX];

struct MyPower {
	int v, p1, p2;
	void set(int value, int point1, int point2) {
		v = value;
		p1 = point1;
		p2 = point2;
	}
};

bool operator<(MyPower a, MyPower b) {
	return (a.v > b.v);
}

int main(void) {
	int i, j, x;
	priority_queue<MyPower> PQ;
	MyPower MP;
	for (i = 0; i < MAX; i++) {
		choice[i] = 0;
	}
	cin >> n;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			cin >> x;
			if (x == -1) continue;
			MP.set(x, i, j);
			PQ.push(MP);
		}
	}
	/*
	int siz = PQ.size();
	for (i = 0; i<siz; i++) {
		MP = PQ.top();
		PQ.pop();
		cout << MP.v<< " "<<MP.p1<<" "<<MP.p2<<endl;
	}
	*/
	
	int c = 1, total = 0;
	while (c < n) {
		MP = PQ.top();
		PQ.pop();
		if (choice[MP.p1] == 0 || choice[MP.p2] == 0) {
			total += MP.v;
			choice[MP.p1] = 1;
			choice[MP.p2] = 1;
			c++;
		}
	}
	cout << total << endl;
	
}