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<bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

using Lint=long long;

class UnionFind
{
 public:
    vector<int> par,sz;
    UnionFind(int N):par(N),sz(N){
        for(int i=0;i<N;i++){
            par[i]=i; sz[i]=1;
        }
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x,int y){
        x=find(x); y=find(y);
        if(x==y) return;
        if(sz[x]<sz[y]) swap(x,y);
        par[y]=x;
        sz[x]+=sz[y];
    }
    bool same(int x,int y){ return find(x)==find(y); }
    int size(int x){ return sz[find(x)]; }
    bool root(int x){ return par[x]==x; }
};

struct edge{
    int u,v,cost;
    bool operator<(const edge &e){
        return cost<e.cost;
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N; cin>>N;
    vector<edge> es;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            int c; cin>>c;
            if(i<j and c!=-1) es.push_back({i,j,c});
        }
    }
    sort(es.begin(),es.end());

    int ans=0;
    UnionFind tree(N);
    for(edge e:es){
        if(!tree.same(e.u,e.v)){
            ans+=e.cost;
            tree.unite(e.u,e.v);
        }
    }
    cout<<ans<<endl;
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
class UnionFind{
  vector<int> node;
 public:
  UnionFind(int n):node(n){
    for(int i = 0;i < n;i++){
      node[i] = i;
    }
  }
  int root(int x){
    if(x == node[x])return x;
    return root(node[x]);
  }
  void merge(int x,int y){
    node[root(x)] = root(y);
  }
};
struct edge{
  int x,y,w;
};
bool operator<(edge e1,edge e2){
  return e1.w < e2.w;
}
int main(){
  int n;
  cin >> n;
  vector<edge> edges;
  UnionFind u(n);
  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      int x;
      cin >> x;
      if(x >= 0){
        edges.push_back({i,j,x});
      }
    }
  }
  int weight = 0;
  sort(edges.begin(),edges.end());
  for(auto &e:edges){
    if(u.root(e.x) == u.root(e.y))continue;
    u.merge(e.x,e.y);
    weight += e.w;
  }
  cout << weight << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX 100
#define WHITE 0
#define BLACK 1
#define INFTY 2001

// グラフの頂点数
int n;
// 隣接行列
vector<vector<int> > M(MAX, vector<int>(MAX));
// 頂点の状態を表す配列
vector<int> color(MAX, WHITE);
// d[v]はvとTの頂点を結ぶ辺の重みの最小値
vector<int> d(MAX, INFTY);
// p[v]はd[v]の重みを持つ辺のvでない方の頂点
vector<int> p(MAX, -1);

int mst()
{
  // 初期化
  int ans = 0;
  d.at(0) = 0;

  while (1)
  {
    // d[u]が最小であるようなV-Tの頂点uを選ぶ
    int u;
    int m = INFTY;
    for (int i = 0; i < n; i++)
    {
      if (color.at(i) != BLACK && d.at(i) < m)
      {
        m = d.at(i);
        u = i;
      }
    }

    if (m == INFTY)
    {
      // MNTが完成したとき
      break;
    }

    // uをTに追加する
    color.at(u) = BLACK;
    // uをTに追加したときの辺の重みをansに足す
    if (p.at(u) != -1)
    {
      ans += M.at(p.at(u)).at(u);
    }

    // uと隣接しているV-Tの各頂点iのd[i]を更新する
    for (int i = 0; i < n; i++)
    {
      if (color.at(i) != BLACK && M.at(u).at(i) != -1 && M.at(u).at(i) < d.at(i))
      {
        d.at(i) = M.at(u).at(i);
        p.at(i) = u;
      }
    }
  }

  return ans;
}

int main()
{
  // 入力
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)
    {
      cin >> M.at(i).at(j);
    }
  }

  // 処理と出力
  cout << mst() << endl;
}