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<stdio.h>
static const int INFTY = (1<<21);
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2


int n,v,i,M[MAX][MAX];
int prim(void);

int main(){
  int j,e;
  scanf("%d",&n);

  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&e);
      M[i][j]=(e==-1)?INFTY:e;
    }
  }
  printf("%d",prim());
  return 0;
}

int prim(){
  int u,minv,sum=0;
  int d[MAX],p[MAX],color[MAX];

  for(i=0;i<n;i++){
    d[i]=INFTY;
    p[i]=-1;
    color[i]=WHITE;
  }

  d[0]=0;

  while(1){
    minv=INFTY;
    u=-1;
    for(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(v=0;v<n;v++){
      if(color[v]!=BLACK&&M[u][v]!=INFTY){
        if(d[v]>M[u][v]){
          d[v]=M[u][v];
          p[v]=u;
          color[v]=GRAY;
        }
      }
    }
  }
  for(i=0;i<n;i++){
    if(p[i]!=-1)sum+=M[i][p[i]];
  }
  return sum;
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
//#include<operator>

using namespace std;


class UFT{
public:
  int P[10010];//その頂点の親を格納する

  //初めは全ての頂点はバラバラで違うグループに属する
  void init(int N){
    for(int i=0;i<N;i++) P[i]=i;
  }

  int root(int a){
    if(P[a]==a) return a;
    return root(P[a]);
  }

  bool is_same_set(int a,int b){
    return root(a)==root(b);
  }

  void unite(int a,int b){
    //aのルートだったもののルートをbのルートにする
    P[root(a)]=root(b);
  }
};

bool is_same_set_all(UFT a,int n){
  for(int i=0;i<n-1;i++){
    if(!(a.is_same_set(i,i+1))){return false;}
  }
  return true;
}

class edge{
public:
  int from_Node;
  int to_Node;
  int cost;
};

//本来は<さえオーバーロードすれば大丈夫が四つのオペレータをオーバロードしなければならなかった(何故？？)
bool operator< (const edge& e1,const edge& e2){
  return e1.cost < e2.cost;
}


bool operator> (const edge& e1,const edge& e2){
  return e1.cost > e2.cost;
}


bool operator<= (const edge& e1,const edge& e2){
  return e1.cost <= e2.cost;
}

bool operator>= (const edge& e1,const edge& e2){
  return e1.cost >= e2.cost;
}


int main(){
  #if 0
  このようにデバッグができる(比較演算子のオーバーロード)
  edge a={0,1,2},b={1,2,3};
  if(a<b){cout << "a<b" << endl;}
  if(b<a){cout << "b<a" << endl;}
  if(a>b){cout << "a>b" << endl;}
  if(a<=b){cout << "a<=b" << endl;}
  #endif


  int n;cin >> n;
  priority_queue<
  edge,//要素の型
  vector<edge>,//内部コンテナはvectorで(デフォ)
  greater<edge>//昇順
  > ve;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      edge e;int c;
      cin >> c;
      if(c!=-1 and i<j){
        e.from_Node=i;e.to_Node=j;e.cost=c;
        ve.push(e);
      }
    }
  }
  UFT G;G.init(n);
  int co=0;
  while(!(is_same_set_all(G,n))){
    if(!(G.is_same_set(ve.top().from_Node,ve.top().to_Node))){
      G.unite(ve.top().from_Node,ve.top().to_Node);
      co+=ve.top().cost;
      ve.pop();
    }else{
      ve.pop();
    }
  }
  cout << co << endl;

}

C++: #include <bits/stdc++.h>
#define MAX 20000
using namespace std;

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n; cin >> n;
  vector< vector<int> > v(n, vector<int>(n));
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> v[i][j];
      if(v[i][j] == -1) v[i][j] = MAX;
    }
  }

  vector<int> cost(n, MAX), used(n, false);
  cost[0] = 0;
  int ans = 0;
  int t;

  while(true){
    t = -1;
    for(int i = 0; i < n; i++){
      if(!used[i] && (t == -1 || cost[t] > cost[i])) t = i;
    }
    if(t == -1) break;
    used[t] = true;
    ans += cost[t];

    for(int i = 0; i < n; i++){
      cost[i] = min(cost[i], v[t][i]);
    }
  }

  cout << ans << endl;

  return 0;
}