Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

int P[10001];

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 (P[a] = root(P[a]));
}

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

void unite(int a, int b) {
	P[root(a)] = root(b);
}

bool span(int n) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (!is_same_set(i, j))
				return false;
		}
	}
	return true;
}

int main() {
	int n;
	cin >> n;
	init(n);
	vector<vector<int> > map(n, vector<int>(n));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> map[i][j];
		}
	}
	vector<pair<int, pair<int, int> > > edges;
	for (int i = 0; i < n; i++) {
		for (int j = i; j < n; j++) {
			if (map[i][j] >= 0)
				edges.push_back(pair<int, pair<int, int> >(map[i][j], pair<int, int>(i, j)));
		}
	}
	sort(edges.begin(), edges.end());
	int result = 0;
	while (!span(n)) {
		pair<int, pair<int, int> > edge = edges.front();
		edges.erase(edges.begin());
		int a = edge.second.first;
		int b = edge.second.second;
		if (!is_same_set(a, b)){
			unite(a, b);
			result += edge.first;
		}
	}
	cout << result << endl;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
using namespace std;

struct Edge{
  int src,dst;
  int weight;

  Edge(int src,int dst,int weight):
    src(src),dst(dst),weight(weight){}
};

bool operator < (const Edge &m,const Edge &n){
  return m.weight != n.weight ? m.weight > n.weight :
    m.src != n.src ? m.src < n.src : m.dst < n.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int MST(priority_queue<Edge>,int);
bool check(int[][100],int,int,int);//src,dstで結ばれる線を追加した時、グラフは閉路かどうかのチェック関数

int main(int argc,char* argv[]){
  int n,val;
  priority_queue<Edge> que;

  cin>>n;
  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      cin>>val;
      que.push(Edge(i,j,val));
    }
  }

  cout<<MST(que,n)<<endl;
  return 0;
}

int MST(priority_queue<Edge> que,int n){
  int ans=0;
  int flag[100][100];
  set<int> s;
  memset(flag,0,sizeof(flag[100][100]));

  while((int)s.size()!=n){
    Edge tmp(0,0,0);
    int ftmp[100][100];
    tmp=que.top();que.pop();
    memcpy(ftmp,flag,sizeof(flag));

    if(tmp.weight!=-1 && flag[tmp.src][tmp.dst]!=1){
      if(check(ftmp,tmp.src,tmp.dst,n)){
	flag[tmp.src][tmp.dst]=1;flag[tmp.dst][tmp.src]=1;
	s.insert(tmp.src);s.insert(tmp.dst);
	ans+=tmp.weight;
      }
    }
  }
  return ans;
}

bool check(int flag[][100],int src,int dst,int n){
  flag[src][dst]=1;
  queue<int> que;
  que.push(src);
  while(!que.empty()){
    int tmp=que.front();que.pop();

    for(int i=1;i<=n;i++){
      if(flag[tmp][i]==1){
	flag[tmp][i]=0;flag[i][tmp]=0;
	que.push(i);
      }
    }
    if(que.front()==src)
      return false;
  }

  return true;
}

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

const int N = 100;
int n;
int A[N][N];
priority_queue< pair<int, int> > Q;

int main()
{
	cin >> n;
	for (int i=0; i<n; i++)
	{
		for (int j=0; j<n; j++)
		{
			cin >> A[i][j];
		}
	}

	int mcost = -INT_MAX;

	for (int i=0; i<n; i++)
	{
		bool visit[N] = {0};

		Q.push(pair<int, int>(0,i));

		int cost = 0;

		while (Q.size())
		{
			pair<int, int> q = Q.top();
			Q.pop();
			int k = q.second;
			if (visit[k]) continue;
			cost += q.first;
			visit[k]=true;
			for(int j=0;j<n;j++)
			{
				int c = A[k][j];
				if (c <= 0) continue;
				if (visit[j]) continue;
				Q.push(pair<int, int>(-c,j));
			}
		}

//		cout << i << ' ' << -cost << endl;

		if (mcost < cost) mcost = cost;
	}

	cout << -mcost << endl;
		
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define repp(i, m, n) for(int i = m; i < n; i++)
const int    INF = 100000000;
const double EPS = 1e-10;
const int    MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int, int> pai;
typedef pair<ll,ll> pal;
int e[101][101];
int t;
pai edge[10001];
int c[10001];
int sz[10001];
int n;
int P[10001];
int ans;
int x, y;
void init(int k)
{
    repp(i, 1, k+1)
    {
        P[i]=i;
        sz[i]=1;
    }
}
int root(int a)
{
    if(P[a]==a)
    {
        return a;
    }
    else
    {
        return (P[a]=root(P[a]));
    }
}
bool is_same_set(int a, int b)
{
    return (root(a)==root(b));
}
void unite(int a, int b)
{
    if(sz[root(a)]<sz[root(b)])
    {
        swap(a, b);
    }
    sz[root(a)]+=sz[root(b)];
    P[root(b)]=root(a);
}
int main()
{
    cin >> n;
    rep(i, n)
    {
        rep(j, n)
        {
            cin >> e[i+1][j+1];
        }
    }
    t=0;
    rep(i, n)
    {
        repp(j, i+1, n)
        {
            if((i<j)&&(e[i+1][j+1]!=-1))
            {
                edge[t]=make_pair(e[i+1][j+1]*1000+i+1, j+1);
                //cout << edge[t].first << ' ' << j+1 << ' ' << t << endl;
                t++;
            }
        }
    }
    sort(edge, edge+t);
    //rep(i, t)
    //{
    //	cout << edge[i].first << ' ' << edge[i].second << endl;
    //}
    rep(i, t)
    {
        c[i]=edge[i].first / 1000;
        edge[i]=make_pair(edge[i].first%1000, edge[i].second);
        // cout << c[i] << ' ' << edge[i].first << ' ' << edge[i].second << endl;
    }
    init(n);
    ans=0;
    rep(i, t)
    {
    if(!is_same_set(edge[i].first, edge[i].second))
        {
            unite(edge[i].first, edge[i].second);
            ans+=c[i];
            // cout << edge[i].first << ' ' << edge[i].second << ' ' << i << ' ' << ans << endl;
        }
    }
    cout << ans << endl;
    return 0;
}