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++: #define dbg(...) fprintf(stderr,__VA_ARGS__)
#define dpr(x) cerr<<#x<<": "<<x<<endl;
#define dprc(c) do{cerr<<#c<<":";for(auto&_i:(c)){cerr<<" "<<_i;}cerr<<endl;}while(0)
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
int INF = 1e9+7;
#define all(c) (c).begin(), (c).end()
#define tr(i,c) for(auto i=(c).begin();i!=(c).end();i++)
#define rtr(i,c) for(auto i=(c).rbegin();i!=(c).rend();i++)
#define rep(i,b) for(auto i=0;i<(b);i++)
#define pb push_back
#define sz(c) int((c).size())

#define MAX_N 110

class UnionFind {
public:
  UnionFind(size_t s) : parent_(s), rank_(s, 0) {
    for (size_t i = 0; i < s; i++) {
      parent_[i] = i;
    }
  }

  void unite(size_t x, size_t y) {
    size_t xp = find(x);
    size_t yp = find(y);

    if (xp == yp) return;
    if (rank_[xp] < rank_[yp]) {
      parent_[xp] = parent_[yp];
    } else {
      if (rank_[xp] == rank_[yp]) rank_[xp]++;
      parent_[yp] = parent_[xp];
    }
  }

  bool is_same(size_t x, size_t y) {
    return find(x) == find(y);
  }

  size_t find(size_t x) {
    if (parent_[x] == x) return x;
    else return parent_[x] = find(parent_[x]);
  }

  size_t size() const {
    return parent_.size();
  }

private:
  vector<int> parent_;
  vector<int> rank_;
};

struct edge {
  int w, v, u;
};

void printuf(UnionFind& uf) {
  for (int i = 0; i < sz(uf); i++) {
    fprintf(stderr, " %-2d", i);
  }
  puts("");
  for (int i = 0; i < sz(uf); i++) {
    fprintf(stderr, " %-2lu", uf.find(i));
  }
  puts("");
}

int main(int argc, char **argv)
{

  int n;
  scanf("%d", &n);

  vector<edge> E;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int x;
      scanf("%d", &x);
      if (i >= j) continue;
      edge e = {x, i, j};
      if (x >= 0) E.pb(e);
    }
  }

  sort(all(E), [=](const edge& lhs, const edge& rhs) { return lhs.w < rhs.w; });

  UnionFind S(n);

  int total = 0;

  for (int i = 0; i < sz(E); i++) {
    edge& e = E[i];

    int v = S.find(e.v);
    int u = S.find(e.u);

    if (v == u) continue;
    total += e.w;
    S.unite(e.v, e.u);
  }

  printf("%d\n", total);

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf (1 << 30)

static const int N = 100;
static const int white = 0;
static const int gray = 1;
static const int black = 2;

int n;
int m[N][N];

int prim() {
	int color[N];
	int d[N], p[N];
	int u;
	for (int i = 0; i < n; i++) {
		color[i] = white;
		d[i] = inf;
		p[i] = -1;
	}
	//color[0] = black;
	d[0] = 0;
	p[0] = -1;
	u = 0;
	while (true) {
		int mincost = inf;
		for (int i = 0; i < n; i++) {
			if (color[i] != black && d[i] < mincost) {
				mincost = d[i];
				u = i;
			}
		}
		if (mincost == inf) {
			break;
		}
		color[u] = black;	
		for (int v = 0; v < n; v++) {
			if (color[v] != black) {
				if (m[u][v] < d[v]) {
					d[v] = m[u][v];
					color[v] = gray;
					p[v] = u;
				}
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (color[i] == black) {
			sum += d[i];
		}
	}
	return sum;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int v;
			cin >> v;
			v == -1 ? m[i][j] = inf : m[i][j] = v;
		}
	}
	prim();
	cout << prim() << endl;
	return 0;
}

C++: #include<iostream>
using namespace std;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int MAXN = 100;
static const int INTFY = (1 << 21);

int n, M[MAXN][MAXN];

int prim()
{
	int u, minv;
	int d[MAXN], p[MAXN], color[MAXN];
	
	for(int i = 0; i < n; i++)
	{
		d[i] = INTFY;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	
	while(true)
	{
		minv = INTFY;
		u = -1;
		for(int i = 0; i < n; i++)
		{
			if(minv > d[i] && color[i] != BLACK)
			{
				u = i;
				minv = d[i];
			}
		}

		if(u == -1) break;
		color[u] = BLACK;

		for(int v = 0; v < n; v++)
		{
			if(color[v] != BLACK && M[u][v] != INTFY)
			{
				if(M[u][v] < d[v])
				{
					d[v] = M[u][v];
					p[v] = u;
					color[v] = GRAY;
				}
			}
		}
	}
	int sum = 0;
	for(int i = 0; i < n; i++)
	{
		if(p[i] != -1)
			sum += M[i][p[i]];
	}
	return sum;	
}


int main ()
{
	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)? INTFY : e; 
		}
	}
	cout << prim() << endl;
	
	return 0;
}