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<algorithm>

using namespace std;

#define INF 2001
#define NIL -2


struct node{
    int key=INF, pi=NIL, flag=0;
};

node grf[101];
int hv[101][101], n;

void search(){
    int min = INF, mini = NIL;
    for(int i=0;i<n;++i){
        if(grf[i].flag != 1 && grf[i].key < min){
            min = grf[i].key;
            mini= i;
        }
    }
    
    if(min == INF){
        return;
    }
    
    grf[mini].flag = 1;
    for(int j=0;j<n;++j){
        if((grf[j].flag != 1 && hv[mini][j] != -1) && hv[mini][j] < grf[j].key){
            grf[j].key = hv[mini][j];
            grf[j].pi = mini;
        }
    }
    
    return;
    
}

int main(){
    int total=0;
    cin >> n;
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            cin >> hv[i][j];
        }
    }
    
    grf[0].key = 0;
    grf[0].flag = 1;
    for(int j=0;j<n;++j){
        if(hv[0][j] != -1 && hv[0][j] < grf[j].key){
            grf[j].key = hv[0][j];
            grf[j].pi = 0;
        }
    }
    
    for(int i=1;i<n;++i){
        search();
    }
    
    for(int i=0;i<n;++i){
        total+=grf[i].key;
    }
    
    cout << total << endl;
    
    return 0;
}

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

int a[100][100];
bool r[100];


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            cin>>a[i][j];
        }
    }
    priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > >q;
    q.push(make_pair(0,0));
    int cost=0;
    pair<int,int> e;
    while(!q.empty()) {
        e=q.top();
        q.pop();
        if(r[e.second]) continue;
        r[e.second]=true;
        cost+=e.first;
        for(int i=0; i<n; i++) {
            if(a[e.second][i]!=-1) q.push(make_pair(a[e.second][i], i));
        }
    }
    cout<<cost<<'\n';
    return 0;
}

C++: #include<iostream>
#include<cstdio>

using namespace std;

#define N 101

int main(){

  int n;

  int g[N][N];
  int min=3000;
  int weight[N];
  int sum=0;

  //load n
  cin >>n;

  //load data
  for(int i=1;i<=n;i++){
    weight[i]=0;
    for(int j=1;j<=n;j++){
      scanf("%d",&g[i][j]);
    }
  }

  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      if(g[i][j]!= -1 && g[i][j]<min) {
	min=g[i][j];
      }
	weight[i]=min;
    }
  }

  for(int i=1;i<=n;i++){
    if(weight[i]!=3000) sum+=weight[i];
  }
  //print
  cout <<sum<<endl;

  return 0;

}

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

#define N 120
int n;

int g[N][N];
int mini;
int weight[N];
bool us[N];

int prim(){
  int v;
  int ans=0;
  weight[1]=0;
  
  while(true){
    v=-1;
   
    for(int i=1;i<=n;i++){
      if(us[i]!=true && (v==-1 ||weight[i]<weight[v]))v=i;
    }

    if(v==-1)break;
    us[v]=true;
    ans+=weight[v];

    for(int i=1;i<=n;i++){
      if(weight[i]>g[v][i])weight[i]=g[v][i];
      else weight[i]=weight[i];
    }
  }
  return ans;
}


int main(){

  //load n
  cin >>n;

  //load data
  for(int i=1;i<=n;i++){
    weight[i]=INT_MAX;
    us[i]=false;
    for(int j=1;j<=n;j++){
      scanf("%d",&g[i][j]);
      if(g[i][j]== -1)g[i][j]=INT_MAX;
      //    cout <<g[i][j]<<" ";
    }
    //    cout <<endl;
  }




  //print
  cout <<prim()<<endl;

  return 0;

}

C++: #include <iostream>
using namespace std;
static const int MAX = 100;
static const int INTFY = (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]=INTFY;
        p[i]=-1;
        color[i]=WHITE;
    }
    
    d[0]=0;
    
    while(1){
        minv=INTFY;
        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] != INTFY){
                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) ? INTFY : e;
        }
    }
    
    cout << prim() << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <string.h>

using namespace std;
typedef pair<int, int> P;
struct edge
{
	int to;
	int cost;
};

vector<edge> G[101];
bool isUsed[101];

void solve()
{
	int n;
	cin >> n;
	vector< vector<int> > data((n), vector<int>(n));
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			int c;
			cin >> data[i][j];
		}
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = i; j < n; ++j)
		{
			if (data[i][j] != -1)
			{
				edge e1, e2;
				e1.to = j;
				e1.cost = data[i][j];
				e2.to = i;
				e2.cost = data[j][i];
				G[i].push_back(e1);
				G[j].push_back(e2);
			}
		}
	}
	memset(isUsed, 0, sizeof(isUsed));
	int ans = 0;
	priority_queue < P, vector<P>, greater<P> > pque;
	pque.push(P(0, 0));
	while (!pque.empty())
	{
		P p = pque.top();
		pque.pop();
		int v = p.second;
		if (isUsed[v])
		{
			continue;
		}
		isUsed[v] = true;
		ans += p.first;
		for (int i = 0; i < G[v].size(); ++i)
		{
			edge e = G[v][i];
			pque.push(P(e.cost, e.to));
		}
	}
	cout << ans << endl;
}

int main()
{
	solve();
	return(0);
}