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>
#include<algorithm>
#define PII pair<int,int>
#define MPII(a,b) make_pair(a,b)
using namespace std;
class NODE
{
public:
	vector<PII> v;
	int index = 0;
};
class Tree
{
public:
	vector<int> V;
	int cost = 0;
};

int main()
{
	NODE* nodes;
	nodes = new NODE[101];
	Tree T;
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			int t;
			cin >> t;
			if (t != -1)
			{
				PII p = MPII(t, j);
				nodes[i].v.push_back(p);
			}
		}
		sort(nodes[i].v.begin(), nodes[i].v.end());
	}
	T.V.push_back(1);
	while (1)
	{
		if (T.V.size() == n+1)break;
		int mincost=10000;
		int addv=-1;
		int index;
		for (auto itr : T.V)
		{
			if (mincost >= nodes[itr].v[nodes[itr].index].first)
			{
				mincost = nodes[itr].v[nodes[itr].index].first;
				addv = nodes[itr].v[nodes[itr].index].second;
				index = itr;
			}
		}
		nodes[index].index++;
		T.V.push_back(addv);
		T.cost += mincost;
	}
	cout << T.cost << endl;
	delete[] nodes;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAX=101;
const int INF=123456789;


class union_find{
public:
  vector<int> rank,pare;
  union_find(){}
  union_find(int size){
    rank.resize(size,0);
    pare.resize(size,0);
    for(int i=0;i<size;i++){
      init(i);
    }
  }

  void init(int x){
    pare[x]=x;
    rank[x]=0;
  }

  int findpare(int x){
    if(pare[x]==x){
      return x;
    }
    else{
      return pare[x]=findpare(pare[x]);
    }
  }

  bool issame(int x,int y){
    return findpare(x)==findpare(y);
  }

  void unite(int x,int y){
    x=findpare(x);
    y=findpare(y);

    if(x==y) return;
    if(rank[x]<rank[y]){
      pare[x]=y;
    }
    else{
      pare[y]=x;
      if(rank[x]==rank[y]) rank[x]++;
    }
  }


};

int n,E;
struct edge{
  int u,v,cost;
};

edge es[MAX];


bool comp(const edge& e1,const edge& e2){
  return e1.cost<e2.cost;
}


int kruskal(){
  sort(es,es+E,comp);//E???????????°
  int res=0;
  union_find uf=union_find(n+1);
  for(int i=0;i<E;i++){
    edge e=es[i];
    if(!uf.issame(e.u,e.v)){
      uf.unite(e.u,e.v);
      res+=e.cost;
    }
  }
  return res;
}

int main(){
  cin>>n>>E;
  for(int i=0;i<E;i++){
    int s,t,w;
    cin>>s>>t>>w;
    es[i].u=s;
    es[i].v=t;
    es[i].cost=w;
  }

  cout<<kruskal()<<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;

struct Node{
  int color, parent;
};

int n, M[MAX][MAX];
Node node[MAX];

void Printnodes(){
  string col[3] = {"WHITE", "GRAY ", "BLACK"};
  for(int i = 0; i < n; i++){
    cout << "nodeID : " << i ;
    cout << " color : " << col[node[i].color];
    cout << " parent : " << node[i].parent << endl;
  }
  cout << endl;
}

int main(){
  cin >> n;

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> M[i][j];
    }
    node[i].color = WHITE;
    node[i].parent = -1;
  }

  node[0].color = BLACK;
  int totaldist = 0;

  while(true){
 //   Printnodes();

    int count = 0, min_d = INFTY, min_id, dist;
    for(int p = 0; p < n; p++){
      if(node[p].color == BLACK){
        for(int i = 0; i < n; i++){
          if(M[p][i] > 0 && node[i].color != BLACK){
            count++;
            node[i].color = GRAY;
            dist = M[p][i];
            if(dist < min_d){
              min_d = dist;
              min_id = i;
              node[i].parent = p;
            }
          }
        }
      }
    }

    if(count == 0)
      break;
    node[min_id].color = BLACK;
    totaldist += min_d;
  }

  cout << totaldist << endl;
  return 0;
}

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

using namespace std;
using MatrixGraph = vector<vector<int>>;

const int no_edege = -1;
const int started_label = 1;
const int no_label = -1;
const int no_cost = -1;
const int max_weight = 2'000;

int total_weight(MatrixGraph matrix_graph, int started_label) {
  vector<int> min_weights(matrix_graph.size(), max_weight + 1);
  vector<int> visited(matrix_graph.size(), false);
  min_weights[started_label - 1] = 0;
  int weight_sum = 0;

  while(true) {
    int min_weight = max_weight + 1;
    int min_label = no_label;
    for(int i = 0; i < matrix_graph.size(); i++) {
      if(visited[i]) {
        continue;
      }
      if(min_weights[i] < min_weight) {
        min_weight = min_weights[i];
        min_label = i + 1;
      }
    }
    if(min_label == no_label) {
      break;
    }

    visited[min_label - 1] = true;
    weight_sum += min_weight;

    for(int i = 0; i < matrix_graph.size(); i++) {
      if(matrix_graph[min_label - 1][i] < min_weights[i]) {
        min_weights[i] = matrix_graph[min_label - 1][i];
      }
    }
  }

  return weight_sum;
}

int main() {
  int vertex_num;
  cin >> vertex_num;
  MatrixGraph matrix_graph(vertex_num, vector<int>(vertex_num, no_edege));

  // Init
  for(int i = 0; i < vertex_num; i++) {
    for(int j = 0 ;j < vertex_num; j++) {
      int weight;
      cin >> weight;
      if(weight == no_edege) {
        matrix_graph[i][j] = max_weight + 1;
        continue;
      }
      matrix_graph[i][j] = weight;
    }
  }

  cout << total_weight(matrix_graph, started_label) << endl;
}

C++: #include <stdio.h>

#define inf 100000
#define WHITE 1
#define BLACK 0
#define MAX 100

int d[MAX],pi[MAX],color[MAX],A[MAX][MAX];
int n,i,j,v,u;
int min,count=0;

int main(){

  int i,j;

  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&A[i][j]);
    }
  }

  for(i=0;i<n;i++){
    d[i]=inf;
    color[i]=WHITE;
  }
  d[0]=0;

  while(1){
    min=inf;
    for(i=0;i<n;i++){
      if(color[i]==WHITE && d[i]<min){
	min=d[i];
	u=i;
      }
    }

    if(min==inf)break;
    color[u]=BLACK;
    count+=min;

    for(v=0;v<n;v++){
      if(A[u][v]==-1)continue;
      if(d[v]>A[u][v])d[v]=A[u][v];
    }
  }

  printf("%d\n",count);

  return 0;
  
}

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

using namespace std; 

int main(void) {
	int nov;
	cin >> nov;
	vector< vector<int> > w(nov,vector<int>(nov));
	for (int i = 0; i < nov; ++i) {
		for (int j = 0; j < nov; ++j) {
			cin >> w[i][j];
		}
	}

	int wmin,v,res = 0;
	vector<int> term(nov,-1); 
	vector<int> check(nov,-1); 
	term[0] = 0;
	check[0] = 1;
	for (int i = 1; i < nov; ++i) {
		wmin = (1 << 28);
		for (int j = 0; j < i; ++j) {
			for (int k = 0; k < nov; ++k) {
				if (check[k] < 0 && w[term[j]][k] >= 0) {
					if (w[term[j]][k] < wmin) {
						wmin = w[term[j]][k];
						v = k;
					}
				}
			}
		}
		res += wmin;
		term[i] = v;
		check[v] = 1;
	}
	cout << res << endl;

	return 0;
}