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>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;

int n;
int m[101][101];
int d[101];
int color[101];
int p[101];

int prim(){
	rep(i, n){
		d[i] = 1e5;
		color[i] = 0;
	}
	d[0] = 0;
	p[0] = -1;
	while(1){
		int mincost = 1e5;
		int u = -1;
		rep(i, n){
			if(d[i] < mincost && color[i] != -1){
				mincost = d[i];
				u = i;
			}
		}
		color[u] = -1;
		if(u == -1) break;
		rep(v, n){
			if(color[v] != -1 && m[u][v] != -1){
				if(m[u][v] < d[v]){
					d[v] = m[u][v];
					p[v] = u;
					color[v] = 1;
				}
			}
		}
	}
	int sum = 0;
	rep(i, n){
		sum += d[i];
	}
	return sum;
}

int main(){
	cin >> n;
	rep(i, n){
		rep(j, n){
			cin >> m[i][j];
		}
	}
	cout << prim() << endl;
}

C++: #include <iostream>

using namespace std;
static const int mx=107;
static const int inf=(1<<21);

int n,m[mx][mx];

int prim()
{
	int u,minv;
	int d[mx],p[mx],color[mx];
	
	for(int i=0;i<n;i++)
	{
		d[i]=inf;
		p[i]=-1;
		color[i]=0;
	}
	d[0]=0;
	
	while (1) {
		minv=inf;
		u=-1;
		for(int i=0;i<n;i++)
		{
			if(minv>d[i]&&color[i]!=2)
			{
				u=i;
				minv=d[i];
			}
		}
		if(u==-1)break;
		color[u]=2;
		for(int v=0;v<n;v++)
		{
			if(color[v]!=2&&m[u][v]!=inf)
			{
				if(d[v]>m[u][v])
				{
					d[v]=m[u][v];
					p[v]=u;
					color[v]=1;
				}
			}
		}
	}
	int sum=0;
	for(int i=0;i<n;i++)
	{
		if(p[i]!=-1)sum+=m[i][p[i]];
	}
	return sum;
}
int main(int argc, char *argv[]) {
	cin>>n;
	for (int i=0;i<n;i++) {
		for (int j=0;j<n;j++) {
			int e;
			cin>>e;
			m[i][j]=(e==-1)?inf:e;
		}
	}
	cout<<prim()<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;
vector<vector<int>> cost;
vector<int> mincost;
vector<bool> used;
int V;

int prim(){
    mincost.assign(V,INF);
    used.assign(V,0);
    mincost[0] = 0;
    int res = 0;
    while(true){
        int v = -1;
        for(int u = 0; u < V; ++u){
            if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
        }
        if(v == -1) break;//completed
        used[v] = 1;
        res += mincost[v];
        for(int u = 0; u < V; ++u){
            if(cost[v][u] != -1)mincost[u] = min(mincost[u], cost[v][u]);//確定した頂点から行ける頂点について、最小コストを更新
        }
    }
    return res;
}


int main(){
    cin >> V;
    cost.assign(V,vector<int>(V));
    rep(i,V)rep(j,V) cin >> cost[i][j];
    int ans = prim();
    cout << ans << endl;
}

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

using namespace std;

class UnionFindTree {
private:
    vector<int32_t> parent, rank;

    int32_t find(int32_t x) {
        if (parent[x] == x) {
            return x;
        } else {
            int32_t p = find(parent[x]);
            parent[x] = p;
            return p;
        }
    }

public:
    UnionFindTree(int32_t n) {
        parent.resize(n);
        rank.resize(n);

        for (int32_t i = 0; i < n; i++) {
            parent[i] = i;
            rank[i] = 0;
        }
    }

    void unite(int32_t x, int32_t y) {
        x = find(x);
        y = find(y);

        if (x == y) return;

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

    bool same(int32_t x, int32_t y) {
        return find(x) == find(y);
    }
};

int main() {
    int32_t n;
    cin >> n;

    UnionFindTree uf(n);
    priority_queue<tuple<int32_t, int32_t, int32_t>, vector<tuple<int32_t, int32_t, int32_t>>, greater<tuple<int32_t, int32_t, int32_t>>> E;

    for (int32_t i = 0; i < n; i++) {
        for (int32_t j = 0; j < n; j++) {
            int32_t w;
            cin >> w;

            if (w > -1) {
                E.push(make_tuple(w, i, j));
            }
        }
    }

    int32_t sum = 0;
    while (!E.empty()) {
        int32_t w, u, v;
        tie(w, u, v) = E.top();
        E.pop();
        if (!uf.same(u, v)) {
            sum += w;
            uf.unite(u, v);
        }
    }

    cout << sum << endl;

    return 0;
}