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 <algorithm>
#include <vector>
using namespace std;
#define INF 1<<30
#define MAX_V 100

int n;
int M[MAX_V][MAX_V];
bool added[MAX_V];
int d[MAX_V], p[MAX_V];

int prim(){
  for(int i = 0; i < n; i++){
    d[i] = INF;
    added[i] = false;
  }

  d[0] = 0; p[0] = -1;

  while(true){
    int minCost = INF, u;
    for(int i = 0; i < n; i++){
      if(!added[i] && d[i] < minCost){
        minCost = d[i];
        u = i;
      }
    }

    if(minCost == INF) break;

    added[u] = true;

    for(int i = 0; i < n; i++){
      if(!added[i] && M[u][i] != -1 && M[u][i] < d[i]){
        d[i] = M[u][i];
        p[i] = u;
      }
    }
  }
  
  int sum = 0;
  for(int i = 0; i < n; i++){
    if(p[i] != -1) sum += M[i][p[i]];
  }

  return sum;
}

int main(void){
  cin >> n;
  for(int i = 0; i < n; i++) for(int j = 0; j < n; j++){
    cin >> M[i][j];
  }
  cout << prim() << endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
#define MAX 100
static const int INFTY=(1<<21);
typedef long long ll;
int n,u;
int color[MAX];
int m[MAX][MAX];
int d[MAX],p[MAX];
int prim(){
    rp(i,n){
        color[i]=0;d[i]=INFTY;p[i]=NIL;
    }
    d[0]=0;
    while(1){
        int mincost=INFTY;int u=-1;
        rp(i,n){
            if(color[i]!=2&&d[i]<mincost){
                mincost=d[i];
                u=i;
            }
        }
        if(u==-1)break;
        color[u]=2;
        rp(v,n){
            if(color[v]!=2&&m[u][v]!=INFTY){
                if(m[u][v]<d[v]){
                    d[v]=m[u][v];
                    p[v]=u;
                    color[v]=1;
                }
            }
        }
    }
    int sum=0;
    rp(i,n)if(p[i]!=NIL)sum+=m[i][p[i]];
    return sum;
}
int main(){
    int a;
    cin>>n;
    rp(i,n){
        rp(j,n){
            cin>>a;
            if(a!=-1)m[i][j]=a;
            else m[i][j]=INFTY;
        }
    }
    cout<<prim()<<endl;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

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

const double EPS = 1e-10;

typedef struct edge_def {
  int from;
  int to;
  int cost;

  edge_def() {;}
  edge_def(int _from, int _to, int _cost) {
    from = _from;
    to   = _to;
    cost = _cost;
  }
} edge;

const int INF = 1000 * 1000 * 1000;
const int N = 100;
int n;
int ed[N];  //ed[i]:??????i???????°???¨?????¨?????¨?????????????????????????????????i??¨??¨?????¨??????????????????????????????
            //????°???¨?????¨????§?????????????????????????????????£????????????
int u[N];  //??¢???????°???¨?????¨???????????????????????????
vector<edge> es[N];

int prim(int s) {
  int i, j, res = 0;
  priority_queue<PII, vector<PII>, greater<PII > > q;
  memset(u, 0, sizeof(u));
  for (i=0; i<n; i++) ed[i] = INF;
  ed[s] = 0;
  q.push(PII(0, s));

  while (q.size()) {
    PII p = q.top(); q.pop();

    u[p.second] = 1;
    res += p.first;

    int m = INF;
    edge me;
    for (i=0; i<n; i++)
      for (j=0; j<(int)es[i].size(); j++) {
        edge e = es[i][j];
        if (u[e.from] && !u[e.to] && e.cost < m) {
          me = e;
          m = me.cost;
        }
      }
    if (m != INF) {
      q.push(PII(me.cost, me.to)); 
      ed[me.to] = me.cost;
    }
  }

  return res;
}


int main() {
  int i, j;
  scanf("%d ", &n);
  int c;
  for (i=0; i<n; i++)
    for (j=0; j<n; j++) {
      scanf("%d ", &c);
      if (c!=-1) es[i].psb(edge(i,j,c));
    }
  printf("%d\n", prim(0));
  return 0;
}

C++: #include<iostream>
using namespace std;

const int MAX=100;
int INFTY=1<<30;
int WHITE=0;
int GRAY=1;
int BLACK=2;

int n,m[MAX][MAX];

void prim()
{
	int u,minv;
	int d[MAX],p[MAX],color[MAX];
	for(int i=0;i<n;i++){
		d[i]=INFTY;
		p[i]=-1;
		color[i]=WHITE;
	}
	d[0]=0;
	while(1)
	{
		minv=INFTY;
		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]!=INFTY)
			if(d[v]>m[u][v]){
				p[v]=u;
				d[v]=m[u][v];
				color[v]=GRAY;
			}
	}
	int sum=0;
	for(int i=0;i<n;i++)
	if(p[i]!=-i) sum+=m[i][p[i]];
	cout<<sum<<endl;
}

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)? INFTY:e;
		}
	}
	prim();
	return 0;
}