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

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF INT_MAX

class Tree
{
    vector<int> rank, id; 
public:
    Tree(int n) : rank(n), id(n) {
        rep(i,n) id[i] = i;
    }
    int getId(int n) {
        if (n != id[n]) id[n] = getId(id[n]);
        return id[n];
    }
    void uniteTree(int u, int v) {
        u = getId(u);
        v = getId(v);
        if (rank[u] > rank[v]) swap(u,v);
        if (rank[u] == rank[v]) rank[v]++;
        id[u] = v;
    }

};

struct Edge
{
    int u, v;
    int cost;
    bool operator<(const Edge& e) const { return this->cost < e.cost; }
};

int main(int, char**)
{
    int n;
    cin >> n;

    vector<Edge> E;
    int tmp;
    rep(i,n) rep(j,n) {
        cin >> tmp;
        if (i >= j) continue;
        if (tmp == -1) continue;
        E.push_back({i,j,tmp});
    }

    sort(E.begin(), E.end());

    Tree tree(n);
    ll ans = 0;
    rep(i,E.size()) {
        int u = E[i].u;
        int v = E[i].v;
        if (tree.getId(u) != tree.getId(v)) {
            tree.uniteTree(u,v);
            ans += E[i].cost;
        }
    }

    cout << ans << endl;

    return 0;
}

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

struct Edge {
  int dst;
  long long cost;
  Edge() {}
  Edge(int x, long long y) : dst(x), cost(y) {}
};

int prim(std::vector<std::vector<Edge>> &G) {
  const int V = (int)G.size();
  std::vector<bool> is_used(V, false);

  typedef std::pair<long long, int> PLI;
  std::priority_queue<PLI, std::vector<PLI>, std::greater<PLI>> que;
  que.push(std::make_pair(0LL, 0));

  int ret = 0;
  while (!que.empty()) {
    long long cost = que.top().first;
    int cur_v = que.top().second;
    que.pop();
    if (is_used[cur_v]) {
      continue;
    }
    is_used[cur_v] = true;
    ret += cost;
    for (int i = 0; i < (int)G[cur_v].size(); i++) {
      que.push(std::make_pair(G[cur_v][i].cost, G[cur_v][i].dst));
    }
  }
  return ret;
}

int main() {
  int n;
  std::cin >> n;

  std::vector<std::vector<Edge>> g(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      std::cin >> a;
      if (a != -1) {
        g[i].push_back(Edge(j, a));
        g[j].push_back(Edge(i, a));
      }
    }
  }
  std::cout << prim(g) << std::endl;

  return 0;
}