Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const (
	MAX_N      = 200
	MAX_WEIGHT = 2000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		adjacentMatrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			v, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjacentMatrix[i][j] = v
		}
	}

	mst([]int{1}, shortestList, adjacentMatrix)
	fmt.Println(calculateMinWeight(shortestList))
}

func calculateMinWeight(shortestList []ShortestInfo) int {
	visitedList := make([]bool, len(shortestList))
	visitedList[0] = true
	sumWeight := 0
	for !visitedAll(visitedList) {
		shortestInfo := shortestList[0]
		shortestList = shortestList[1:]
		edges := shortestInfo.edges
		for _, edge := range edges {
			if !visitedList[edge.end-1] {
				visitedList[edge.end-1] = true
				sumWeight += edge.weight
			}
		}
	}
	return sumWeight
}

func visitedAll(visitedList []bool) bool {
	for _, v := range visitedList {
		if !v {
			return false
		}
	}
	return true
}

func mst(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			s := getShortest(adj.end, shortestList)
			if currentShortest.shortest+adj.weight < s.shortest || (currentShortest.shortest+adj.weight == s.shortest && len(currentShortest.edges)+1 > len(s.edges)) {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node-1]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node-1] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode-1]
	edges := []Edge{}
	for i, v := range adjacents {
		if v > 0 {
			edges = append(edges, Edge{start: currentNode, end: i + 1, weight: v})
		}
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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