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 <algorithm>
using namespace std;
struct edge{
    int u,v,cost;
    bool operator<(const edge& v) const {
        return cost < v.cost;
    }
};
const int INF = 1001001001;
const edge imEg = {-1,-1,INF};
vector<edge> es(10000, imEg);
int V,E;
vector<int> par;
vector<int> rnk;

void init_union_find(int n) {
    par.resize(n);
    rnk.resize(n);
    for(int i=0; i<n; i++) {
        par[i] = i;
        rnk[i] = 0;
    }
}

int find(int x) {
    if(x != par[x]) par[x] = find(par[x]);
    return par[x]; 
}

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(rnk[x] > rnk[y]) par[y] = x;
    else par[x] = y;
    if(rnk[x] == rnk[y]) rnk[y]++;
}

int kruskal() {
    init_union_find(V);
    sort(es.begin(), es.end());
    int res = 0;
    for(int i=0; i<E; i++) {
        edge e = es[i];
        if(find(e.u) != find(e.v)) {
            unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main() {
    cin >> V;
    E = 0;
    for(int i=0; i<V; i++) {
        for(int j=0; j<V; j++) {
            int w;
            cin >> w;
            if(w != -1) {
                struct edge e = {i,j,w};
                es[E] = e;
                E++;
            }
        }
    }
    cout << kruskal() << endl;
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

struct Edge{
	int dist,cost;
	Edge(){}
	Edge(int dist,int cost):dist(dist),cost(cost){}
};
	bool operator<(const Edge& e1, const Edge& e2){
		return e1.cost > e2.cost;
	}


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
	int n;cin>>n;
	vector<vector<Edge> > edge(n,vector<Edge>());
	REP(i,n){
		REP(j,n){
			int c;
			cin>>c;
			if(c == -1)continue;
			edge[i].push_back(Edge(j,c));
		}
	}
	priority_queue<Edge,vector<Edge>,less<Edge> > que;
	que.push(Edge(0,0));//from 0
	int ct = 0;
	int ans = 0;
	vector<bool> primed(n,false);
	while(ct < n){
		Edge e = que.top();
		que.pop();
		if(primed[e.dist])continue;
		primed[e.dist] = true;
		ct++;
		ans += e.cost;
		for(auto e2 : edge[e.dist]){
			que.push(e2);
		}
	}
	cout << ans << endl;
  return 0;
}

C++: /*
* @Author: Samson
* @Date:   2018-06-12 18:24:44
* @Last Modified by:   Samson
* @Last Modified time: 2018-06-13 12:52:45
*/
//   @URL : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
#include<bits/stdc++.h>
#include<algorithm>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e2+10;

int p[MAXN],color[MAXN],a[MAXN][MAXN],t,n,d[MAXN];

int prim()
{
	int minv,u,v;
	memset(p,-1,sizeof p);
	memset(d,INF,sizeof d);

	d[2] = 0;
	int ans = 0;

	while(1)
	{
		minv = INF;
		u = -1;
		for(int i = 1; i <= n; ++i)
		{
			if(minv > d[i] && !color[i])
				minv = d[i],u = i;
		}
		if(u == -1)	break; //空集
		color[u] = 1; //从集合中删除
		for(int v = 1; v <= n; ++v)
		{
			if(!color[v] && a[u][v] != INF)
			{
				if(d[v] > a[u][v])
				{
					d[v] = a[u][v];
					p[v] = u;
				}
			}
		}
	}

	for(int i = 1; i <= n; ++i)
		if(p[i] != -1)
			ans += a[p[i]][i];
	return ans;
}

int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	cin>>n;
	for(int i = 1; i <= n; ++i)
	{
		for(int j = 1; j <= n; ++j)
		{
			cin>>t;
			a[i][j] = t==-1?INF:t;
		}
	}
	cout<<prim()<<'\n';
	return 0;
}

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

#define MAX_V 10000
#define INF 1e9

typedef pair<int, int> pii; // <cost, to>

int cost[MAX_V][MAX_V]; // cost[u][v]??????e=(u,v)????????????(?????¨???????????´??????INF)
bool used[MAX_V]; // ??????i???X????????????????????????
int V;

int prim() {
  for (int i = 0; i < V; ++i) {
    used[i] = false;
  }
  std::priority_queue<pii, vector<pii>, greater<pii> > que;
  que.push(pii(0, 0));
  int res = 0;

  while (!que.empty()) {
    int cst = que.top().first, v = que.top().second;
    que.pop();
    if (used[v])
      continue;
    used[v] = true;
    res += cst;
    for (int u = 0; u < V; u++)
      que.push(pii(cost[v][u], u));
  }

  return res;
}

int main(){
    cin >> V;
    for(int i=0;i<V;++i){
        for(int j=0;j<V;++j){
            int a;
            cin >> a;
            cost[i][j] = (a == -1 ? INF : a);
        }
    }
    cout << prim() << endl;

    return 0;
}