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>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

class UF {
private:
    int sz; vector<int> par,nrank;
public:
    UF(){}
    UF(int node_size){ sz = node_size; par.resize(sz),nrank.resize(sz); rep(i,sz){ par[i] = i; nrank[i] = 0; } }
    int find(int x){ if(par[x] == x){ return x; }else{ return par[x] = find(par[x]); } }
    void unite(int x,int y)
    { x = find(x),y = find(y); if(x == y) return;
    	if(nrank[x] < nrank[y]){ par[x] = y; }else{ par[y] = x; if(nrank[x] == nrank[y]) nrank[x]++; } }
    bool same(int x,int y){ return find(x) == find(y); }
};

template<typename T> class Kruskal{
public:
	struct edge{
		int u,v;
		T cost;
		bool operator<(const edge& another) const {
			return cost < another.cost;
		}
	};
	vector<edge> es;
	vector<int> par,rrank;
	int V;
    Kruskal(int node_size) : V{node_size}, par{node_size}, rrank{node_size}{
		// V = node_size;
		// par.resize(node_size),rrank.resize(node_size);
	}
	void add_edge(int u,int v,T cost){
		es.push_back((edge){u,v,cost});
	}
	T make(){
		UF uf(V);
		T res = 0;
		int cnt = 0;
		rep(i,es.size()){
			edge e = es[i];
			if(!uf.same(e.u,e.v)){
				uf.unite(e.u,e.v);
				res += e.cost;
				cnt++;
				if(cnt == V-1){
					break;
				}
			}
		}
		return res;
	}
};


int main()
{
    int n;
    cin >> n;
    Kruskal<int> kr(n);
    rep(i,n){
        rep(j,n){
            int a;
            cin >> a;
            if(i < j){
                kr.add_edge(i,j,a);
            }
        }
    }
    cout << kr.make() << "\n";
}

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

#define MAX_NODE 100
#define INF 1000000000

using namespace std;

struct Edge{
	int to, w;
};

vector <Edge> e[MAX_NODE];

int minw[MAX_NODE];
bool flag[MAX_NODE];

int prim(int n,int start){
	fill(minw,minw+n,INF);
	fill(flag,flag+n,false);
	minw[start]=0;
	int sum=0;
	int from = start;
	while(1){
		for(int i=0;i<e[from].size();i++){
			if(e[from][i].w<minw[e[from][i].to]){
				minw[e[from][i].to]=e[from][i].w;
			}		
		}
		int minimum_e;
		minimum_e=INF;
		for(int i=0;i<n;i++){
			if(minimum_e>minw[i]&&flag[i]==false){
				minimum_e=minw[i];
				from=i;
			}
		}
		if(minimum_e==INF){
			break;
		}
		sum+=minimum_e;
		flag[from]=true;
	}
	return sum;
}

int main(){
	int n,tmp,sum;
	Edge edge;
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>tmp;
			if(tmp!=-1){
				edge.to=j;
				edge.w=tmp;
				e[i].push_back(edge);
			}
		}
	}
	sum=prim(n,0);
	cout<<sum<<endl;
	return 0;
}

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

const int INF = 1 << 30;

struct Vertex {
	int id;
	int w;
	vector<Vertex> vtxs;
};

int prim(int n, Vertex vtxs[]){
	
	int d[n];
	bool vst[n];
	for(int i=0;i<n;i++){
		d[i] = INF;
		vst[i] = false;
	}
	
	d[0]=0;
	while(true) {
		int minv = INF;
		int u = -1;
		for(int i=0;i<n;i++){
			if(vst[i]) continue;
			if(minv > d[i]){
				minv = d[i];
				u = i;
			}
		}
		if(u==-1) break;
		vst[u]=true;
		vector<Vertex> vec=vtxs[u].vtxs;
		for(int v=0;v<vec.size();v++){
			Vertex vtx=vec[v];
			int id=vtx.id;
			if(vst[id]) continue;
			d[id]=min(d[id],vtx.w);
		}
	}
	int cost=0;
	for(int i=0;i<n;i++){
		cost+=d[i];
	}
	return cost;
}

int main(){
	int n;
	cin >> n;
	Vertex vtxs[n];
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int w;
			cin >> w;
			if(w==-1) continue;
			Vertex v;
			v.id = j;
			v.w = w;
			vtxs[i].vtxs.push_back(v);
		}
	}
	int cost = prim(n,vtxs);
	cout << cost << endl;
	return 0;
}