Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 2000200
#define MIN -2000000300
#define INF 1<<30
using namespace std;
int n;
int A[1000][1000];
int d[1000];
bool used[1000];

void Init(int n){
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      A[i][j] = INF;
    }
  }
}

int prime(){
  for(int i=0;i<n;i++){
    d[i] = INF;
    used[i]=false;
  }
  int ans=0;
  d[0] = 0;

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

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

  }
  return ans;
}

int main(){
  scanf("%d",&n);
  //  Init(n);
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      scanf("%d",&A[i][j]);
      if(A[i][j] == -1)A[i][j] = INF;
    }
  }
 
  printf("%d\n",prime());
 

  return 0;
}

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

#define N 101
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY (1<<21)

int n, M[N][N], color[N], d[N], p[N];

int prim(){
    int mincost, u, sum = 0;
    for (int i = 1; i <= n; i++){
        color[i] = WHITE;
        d[i] = INFTY;
    }
    d[1] = 0;
    p[1] = -1;
    
    while(1){
        mincost = INFTY;
        for (int i = 1; i <= n; i++){ 
            if ( color[i] != BLACK && d[i] < mincost ){
                mincost = d[i];
                u = i;
            }
        }
        
        if ( mincost == INFTY ) break;
        
        color[u] = BLACK;
        
        for (int v = 1; v <= n; v++){
            if ( color[v] != BLACK && M[u][v] != -1 ){
                if ( M[u][v] < d[v] ){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    
    for (int i = 1; i <= n; i++){
        if ( p[i] != -1 ) sum += M[i][p[i]];
    }
    return sum;
}

int main(){
    cin >> n;
    for (int i = 1; i <= n; i++){ //隣接行列の生成
        for (int j = 1; j <= n; j++){
            cin >> M[i][j];
            M[j][i] = M[i][j];
        }
    }
    
    cout << prim() << endl;
    
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <functional>
#include <vector>
using namespace std;

#define V vector<vector<pair<int,int>>>

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	int n,Ans = 0; cin >> n;
	V v(2001);
	vector<bool> f(n + 1);
	for (int i = 1; i <= n;i++) {
		for (int j = 1; j <= n; j++) {
			int a; cin >> a;
			if (a != -1) {
				v[a].push_back(make_pair(i, j));
			}
		}
	}

	for (int i = 0; i <= 2000;i++) {
		for (int j = 0; j < v[i].size(); j++) {
			int first = v[i][j].first, second = v[i][j].second;
			if (f[first] == true && f[second] == true) continue;

			Ans += i;
			f[first] = f[second] = true;
		}
	}

	cout << Ans << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
using P = pair<int, int>;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

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

    return sum;
}

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;
        }
    }

    cout << prim() << endl;

    return 0;
}

C++: #include <stdio.h>
#define INF 999999

int n, k;
int color[3000], d[3000], fathernode[30];
int map[3000][3000];

int dijkstra(int s)
{
    for(int i = 0; i < n; i ++)
    {
        d[i] = INF;
        fathernode[i] = -1;
    }
    d[s] = 0;
    while(1)
    {
        int min = INF;
        int u = -1;
        for(int i = 0; i < n; i ++)
        {
            if(min > d[i] && color[i] != 1)
            {
                u = i;
                min = d[i];
            }
        }
        if(u == -1)
            break;
        color[u] = 1;//染色，以防后面重复使用
        for(int i = 0; i < n; i ++)
        {
            if(color[i] != 1 && map[u][i] != INF)
                if(d[i] > d[u] + map[u][i])
                    d[i] = d[u] + map[u][i];
        }
    }
    return 0;
}

int main(void)
{
    int x, y, v;
    scanf("%d", &n);
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < n; j ++)
            map[i][j] = INF;
    for(int i = 0; i < n; i ++)
    {
        scanf("%d%d", &x, &k);
        for(int j = 0; j < k; j ++)
        {
            scanf("%d%d", &y, &v);
            map[x][y] = v;
        }
    }
    dijkstra(0);
    
    for(int i = 0; i < n; i ++)
    {
        if(d[i] != INF)
            printf("%d %d", i, d[i]);
        printf("\n");
    }
    return 0;
}