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(shortestList)
	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 <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <list>
#include <bitset>

using namespace std;
typedef long long ll;

// #define DEBUG

class Unionfind
{
    vector<int> p;

public:
    Unionfind(int n);
    int find(int x);
    void unite(int x, int y);
    bool match(int x, int y);
};

Unionfind::Unionfind(int n)
{
    for (int i = 0; i < n; i++)
    {
        p.push_back(i);
    }
}

int Unionfind::find(int x)
{
    while (p[x] != x)
    {
        p[x] = p[p[x]];
        x = p[x];
    }
    return x;
}

bool Unionfind::match(int x, int y)
{
    return (find(x) == find(y)) ? true : false;
}

void Unionfind::unite(int x, int y)
{
    x = Unionfind::find(x);
    y = Unionfind::find(y);
    if (x != y)
    {
        p[x] = y;
    }
}

int main()
{
#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, d, x, y, ans;
    cin >> n;
    Unionfind tree(n);
    priority_queue<tuple<int, int, int>> pq;
    tuple<int, int, int> w;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> x;
            if (x != -1)
            {
                pq.push(make_tuple(-x, i, j));
            }
        }
    }

    ans = 0;
    while (!pq.empty())
    {
        w = pq.top();
        d = -get<0>(w), x = get<1>(w), y = get<2>(w);
        pq.pop();
        if (!tree.match(x, y))
        {
            tree.unite(x, y);
            ans += d;
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;

struct Data{

  int weight;
  int s,g;

  bool operator < (const Data& p)const {
    if(weight != p.weight)return weight > p.weight;
    if(s != p.s)return s > p.s;
    return g > p.g;
  }
  
};

int par[100],rank[100];
priority_queue<Data> data;

int find(int x){
  
  if( x == par[x]){
    return x;
  }else{
    par[x]=find(par[x]);
  }
  return par[x];
}
bool same(int x,int y){
  if(find(x) == find(y)){
    return true;
  }else{
    return false;
  }
}
void unite(int x,int y){
  
  int x_oya,y_oya;
  
  x_oya=find(x);
  y_oya=find(y);
  
  if(rank[x_oya] > rank[y_oya]){
    par[x_oya]=par[y_oya];
  }else if(rank[x_oya] < rank[y_oya]){
    par[y_oya]=par[x_oya];
  }else{
    par[y_oya]=par[x_oya];
    rank[x_oya]++;
  }
  
}
int kruskal(void){
  
  Data a;
  int cost=0;
  
  while(!data.empty()){
    a=data.top();
    data.pop();
    if(!same(a.s,a.g)){
      cost+=a.weight;
      unite(a.s,a.g);
    }
  }
  return cost;
  
}
int main(){
  
  int n,weight;
  
  for(int i=0 ; i < 100 ; i++){
    par[i]=i;
    rank[i]=0;
  }
  
  cin >>n;

  for(int i=0 ; i < n ; i++){
    for(int j=0 ; j < n ; j++){
      cin >>weight;
      if(weight == -1)continue;
      data.push((Data){weight,i,j});
    }
  }
  
  cout <<kruskal()<<endl;

  return 0;
}

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

const int N=105;
int g[N][N];
int dist[N];
bool vis[N];
int n, ans=0;

void prim(){
    dist[0]=0;
    for(int i=0; i<n; ++i){
        int node=-1;
        for(int j=0; j<n; ++j){
            if(!vis[j] && (node==-1 || dist[j]<dist[node])) node=j;
        }
        if(node==-1) break;
        vis[node]=true;
        ans+=dist[node];
        for(int j=0; j<n; ++j) 
            if(g[node][j]!=-1) dist[j]=min(dist[j], g[node][j]);
    }
}

int main(){
    memset(dist, 0x3f, sizeof dist);
    memset(vis, 0x00, sizeof vis);
    cin>>n;
    for(int i=0; i<n; ++i)
        for(int j=0; j<n; ++j)
            cin>>g[i][j];
            
    prim();
    cout<<ans<<endl;
    return 0;
}