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 <iostream>
#include <vector>
#include <climits>
#include <set>

class Graph
{
 private:
  std::vector<std::vector<long> > weight_;
 public:
  Graph(const unsigned long node_num){};


  void insert_weight(const std::vector<long> weight){
    weight_.push_back(weight);
  };


  void display()
  {
    for(unsigned long i = 0, imax = weight_.size(); i < imax; ++i)
    {
      for(unsigned long j = 0, jmax = weight_[i].size(); j < jmax; ++j)
      {
        std::cout << " " << weight_[i][j];
      }
      std::cout << std::endl;
    }
  };


  unsigned long MinimumSpanningTree()
  {
    if(weight_.size() <= 1) return 0;

    struct Edge
    {
      long value_;
      long start_node_;
      long end_node_;
      Edge(long value = LONG_MAX, long sn = -1, long en = -1) : value_(value), start_node_(sn), end_node_(en){};
      void change(long value, long sn, long en)
      {
        value_ = value; start_node_ = sn, end_node_ = en;
      };
      void display()
      {
        std::cout << value_ << " : " << start_node_ << " : " << end_node_ << std::endl;
      };
    };

    unsigned long minimum_spanning_value = 0;

    std::set<long> connect_node; connect_node.insert(0);
    std::set<long> noconnect_node;

    for(unsigned long node_i = 1, imax = weight_.size(); node_i < imax; ++node_i)
      noconnect_node.insert(node_i);

    while(!noconnect_node.empty())
    {
      Edge min_edge;
      for(std::set<long>::iterator it = connect_node.begin(); it != connect_node.end(); ++it)
      {
        for(std::set<long>::iterator noit = noconnect_node.begin(); noit != noconnect_node.end(); ++noit)
        {
          if(weight_[*it][*noit] == -1) continue;
          if(weight_[*it][*noit] < min_edge.value_)
          {
            min_edge.value_ = weight_[*it][*noit];
            min_edge.change(weight_[*it][*noit], *it, *noit);
          }
        }
      }
      connect_node.insert(min_edge.end_node_);
      noconnect_node.erase(min_edge.end_node_);
      minimum_spanning_value += min_edge.value_;
    }

    return minimum_spanning_value;
  };//   unsigned long MinimumSpanningTree()
 protected:
};//    class Graph

int main()
{

  unsigned long node_num = 0;
  std::cin >> node_num; std::cin.ignore();

  Graph graph(node_num);

  for(unsigned long node_i = 0; node_i < node_num; ++node_i)
  {
    std::vector<long> weight(node_num, -1);
    for(unsigned long weight_i = 0, weight_num = node_num; weight_i < weight_num; ++weight_i)
    {
      std::cin >> weight[weight_i]; std::cin.ignore();
    }
    graph.insert_weight(weight);
  }

  std::cout << graph.MinimumSpanningTree() << std::endl;

  return 0;
};//    main()

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <tuple>
#include <sstream>

struct Node
{
  int data;
  Node* parent;
  int rank;
  Node() : data(-1), parent(this), rank(0){}
  Node& operator=(int data){ this->data = data; return *this; }
  Node* Find(){
    if (this == this->parent){
      return this;
    } else {
      auto root = this->parent->Find();
      this->parent = root;
      return root;
    }
  }
  static void Union(Node& lhs, Node& rhs){
    Node* lhs_root = lhs.Find();
    Node* rhs_root = rhs.Find();
    if (lhs_root->rank > rhs_root->rank){
      rhs_root->parent = lhs_root;
    } else if (rhs_root->rank > lhs_root->rank){
      lhs_root->parent = rhs_root;
    } else if (rhs_root != lhs_root){
      lhs_root->parent = rhs_root;
      rhs_root->rank += 1;
    }
    return;
  }
};

bool is_not_space(int c){ return c != ' '; }
std::string& ltrim(std::string& str){
  str.erase(str.begin(), std::find_if(str.begin(), str.end(), is_not_space));
  return str;
}
std::string& rtrim(std::string& str){
  str.erase(std::find_if(str.rbegin(), str.rend(), is_not_space).base(), str.end());
  return str;
}
std::string trim(std::string& str){ return ltrim(rtrim(str)); }

struct Edge{
  int i,j,w;
  bool exist;
  Edge(int i,int j,int w): i(i), j(j), w(w), exist(true){ if(w==-1){ this->exist = false; } }
  int Value(){ return this->exist ? this->w : 2005; }
};
int main(void){
  std::string line;
  std::getline(std::cin, line);
  int n = std::stoi(line);
  std::vector<Node> V(n);
  std::vector<Edge> E;
  for(int i = 0; i < n; ++i){
    std::getline(std::cin, line);
    std::stringstream ss(trim(line));
    for(int j = 0; j < n; ++j){
      int w;
      ss >> w;
      E.emplace_back(i,j,w);
    }
  }
  int sum_w = 0;
  for(int remain = n-1; remain > 0;){
    auto min_elem = std::min_element(E.begin(), E.end(), [](auto& lhs, auto& rhs){ return lhs.Value() < rhs.Value(); });
    min_elem->exist = false;
    E[min_elem->j * n + min_elem->i].exist = false;
    if(V[min_elem->i].Find() != V[min_elem->j].Find()){
      Node::Union(V[min_elem->i], V[min_elem->j]);
      sum_w += min_elem->w;
      remain -= 1;
    }
  }
  std::cout << sum_w << std::endl;
  return 0;
}