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 <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

constexpr int INF = 3000;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, aij, mincost, minu, sum=0;
    std::cin >> n;
    std::vector<std::vector<int>> a(n);
    std::vector<int> d(n), p(n), color(n); // white:0, gray:1, black:2
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            std::cin >> aij;
            a[i].push_back(aij);
        }
        d[i] = INF;
    }
    d[0]=0;
    p[0]=-1;
    while (true) {
        mincost = INF;
        for (int i=0; i<n; i++) {
            if (color[i] < 2 and d[i] < mincost) {
                mincost = d[i];
                minu = i;
            }
        }
        if (mincost == INF) {
            break;
        }
        color[minu] = 2;
        for (int v=0; v<n; v++) {
            if (color[v] < 2 and a[minu][v] >= 0 and d[v] > a[minu][v]) {
                d[v] = a[minu][v];
                p[v] = minu;
                color[v] = 1;
            }
        }
    }

    for (int i=0; i<n; i++) {
        sum += d[i];
    }
    std::cout << sum << "\n";

    return 0;
}

C++: #include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)

int main(void) {
  int n, i, j, ans = 0;
  scanf("%d", &n);
  int a[n][n];
  char f[n];
  rep(i, n) rep(j, n) scanf("%d", &a[i][j]);
  rep(i, n) f[i] = 0;
  f[0] = 1;
  rep(i, n - 1) {
    int min = 10000, minx, miny;
    rep(j, n) {
      rep(k, n) {
        if(f[j] && !f[k] && a[j][k] > 0 && a[j][k] < min) min = a[j][k], minx = j, miny = k;
      }
    }
    ans += min;
    f[miny] = 1;
  }
  printf("%d\n", ans);
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <utility>
#include <functional>
#include <vector>
using namespace std;
int cost[100][100];
int prim(int n,int s){
	typedef pair<int,int> P;
	bool ischeck[n];
	int mincost = 0;
	priority_queue<P,vector<P>,greater<P> > pq;
	P p,tp;
	for(int i = 0;i < n;++i){
		ischeck[i] = false;
		if(cost[s][i] != -1){
			p.first = cost[s][i];
			p.second = i;
			pq.push(p);
		}
	}
	ischeck[s] = true;
	while(!pq.empty()){
		p = pq.top();
		pq.pop();
		if(ischeck[p.second]) continue;
		else{
			ischeck[p.second] = true;
			mincost += p.first;
			for(int i = 0;i < n;++i){
				if(cost[p.second][i] != -1){
					tp.first = cost[p.second][i];
					tp.second = i;
					pq.push(tp);
				}
			}
		}
	}
	return mincost;
}
int main(){
	int n;
	cin >> n;
	for(int i = 0;i < n;++i){
		for(int j = 0;j < n;++j){
			cin >> cost[i][j];
		}
	}
	cout << prim(n,0) << endl;
}

C++: #include <stdio.h>
#include <limits.h>
int n,M[100][100];
int prim();
int main(){
  int i,j,w;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&w);
      if(w==-1)M[i][j]=INT_MAX;
      else M[i][j]=w;
    }
  }
  printf("%d\n",prim());
  return 0;
}
int prim(){
  int sum,i,u,mincost,C[100],d[100],p[100];
  for(i=0;i<n;i++){
    d[i]=INT_MAX;
    C[i]=0;
    p[i]=-1;
  }
  d[0]=0;
  while(1){
    u=-1;
    mincost=INT_MAX;
    for(i=0;i<n;i++){
      if(C[i]!=2&&d[i]<mincost){
        mincost=d[i];
        u=i;
      }
    }
    if(u==-1)break;
    C[u]=2;
    for(i=0;i<n;i++){
      if(C[i]!=2&&M[u][i]!=INT_MAX){
        if(d[i]>M[u][i]){
          d[i]=M[u][i];
          p[i]=u;
          C[i]=1;
        }
      }
    }
  }
  sum=0;
  for(i=0;i<n;i++){
    if(p[i]!=-1)sum+=M[i][p[i]];
  }
  return sum;
}