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++: //最小生成树Prim算法
#include <iostream>

using namespace std;

const int N = 100;
const int INFTY = 1 << 14; //赋值无穷大

int n; //图的规模

short  Graph[N][N];

int *p; //p[i]记录着它们之间最小权值那个路径上，i的父节点
int *d;//d[i]标记着从i的父节点到i所需的最小权值
char *color; //标记节点是否访问状态


void prim() { //普莱姆算法
    for (int i=0;i<n;i++) {
    	d[i] = INFTY;
    	color[i] = 'W';
	}
	//普莱姆算法选取任意点作为开始点，这里我们选取第一个点 
	p[0] = INFTY;
	d[0] = 0;
	
	while (1) {
		
		//每次从剩余点中选取权值最小的点 
		int mincost = INFTY;
		int u;
		for (int i=0;i<n;i++) {
			if (color[i] != 'B' && mincost > d[i]) {
				mincost = d[i];
				u = i;
			}
		}
		
		if (mincost == INFTY) { //说明所有节点已经访问完毕 
			break;
		}
		
		color[u] = 'B'; //标记节点已经访问结束 
		
		for (int v=0;v<n;v++) {
			if (color[v] != 'B' && Graph[u][v] != INFTY) {
				if (d[v] > Graph[u][v]) {
					d[v] = Graph[u][v];
					p[v] = u;
					color[v] = 'G';
				}
			}
		}
	}
	
}

int main() {
	cin >> n;
	p = new int[n];
	d = new int[n];
	color = new char[n];

	for (int i=0; i<n; i++) {
		for (int j = 0; j<n; j++) {
			cin >> Graph[i][j];
			if (Graph[i][j] == -1) {
				Graph[i][j] = INFTY;
			}
		}
	}
	
	prim();
	int sum = 0;
	for (int i=0; i<n; i++) {
		if (p[i] != INFTY) {
			sum += Graph[i][p[i]];
		}
	}
	cout << sum << endl;
	delete[] p;
	delete[] d;
	delete[] color;
	return 0;
}

C++: //専用ライブラリ
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll>P;
#define INF 10e16
#define MOD 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define fi first
#define se second
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define PI acos(-1.0)
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int cmp(P a,P b){
        if(a.fi!=b.fi)
        return a.fi>b.fi;
        else
        return a.se<b.se;
}
//---------------------------------------------------------------------------
#define INFTY 2001
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 100

// 最小全域木を構成し、重みの総和を返す関数
int prim(int n, int A[N][N]) {
  int i, vertex, min_cost, sum_weight, cost[N], color[N];
  // 初期化
  for (i = 0; i < n; i++) {
    cost[i] = INFTY;
    color[i] = WHITE;
  }
  cost[0] = 0;
  color[0] = GRAY;
  sum_weight = 0;
  
  while (1) {
    // Tに隣接する頂点の中で最小のコストを持つものを選ぶ
    vertex = -1;
    min_cost = INFTY;
    for (i = 0; i < n; i++) {
      if (color[i] == GRAY && cost[i] < min_cost) {
        min_cost = cost[i];
        vertex = i;
      }
    }
    // T = V ならば終了
    if (vertex == -1) break;

    color[vertex] = BLACK;  // vertexをTに加える
    sum_weight += cost[vertex];

    // vertexに隣接し、Tに属していない頂点のコストを更新
    for (i = 0; i < n; i++) {
      if (color[i] != BLACK && A[vertex][i] < INFTY) {
        color[i] = GRAY;
        if (cost[i] > A[vertex][i]) cost[i] = A[vertex][i];
      }
    }
  }

  return sum_weight;
}

/*
// 最小全域木の辺の重みの和を計算する関数
int sum_weight(int n, int parent[], int A[N][N]) {
  int sum = 0;
  
  for (int i = 1; i < n; i++) sum += A[i][parent[i]];
  
  return sum;
}*/

//---------------------------------------------------------------------------
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    int i, j, n, w, A[N][N];
    cin >> n;
    // 隣接行列が与えられる
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        cin >> w;
        if (w == -1) A[i][j] = INFTY;
        else A[i][j] = w;
      }
    }

    cout << prim(n, A) << endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool f[100];
int n;
int tr[100];


int trmin(){
  int res=10000,resi;
  for(int i=0;i<n;i++)
    if(f[i]==0 && tr[i]<res) res = tr[i],resi=i;
  return resi;
}


int main (){

  int mp[100][100];

  cin >> n;
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      cin >> mp[i][j];

  for(int i=0;i<n;i++) f[i]=0;
  for(int i=0;i<n;i++) tr[i]=10000;
  tr[0] = 0;
  int trsum=0;

  for(int k=0;k<n;k++){
    int r=trmin();
    f[r]=1;
    trsum+=tr[r];

    for(int i=0;i<n;i++) 
      if(mp[r][i]!=-1)
	tr[i] = min(tr[i],mp[r][i]);
  }
  cout<<trsum<<endl;

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF 2*1e9
int main(){
  
  int n;
  int cost[100][100];
  int mincost[100];
  int used[100]={};
  
  cin>>n;
  for(int i=0;i<n;i++){
    mincost[i]=INF;
    for(int j=0;j<n;j++){
      cin>>cost[i][j];
      if(cost[i][j]==-1)cost[i][j]=INF;
    }
  }

  mincost[0]=0;
  int ans=0;

  while(1){
    int v=-1;
    for(int u=0;u<n;u++){
      if(used[u]==0&&(v==-1||mincost[u]<mincost[v]))v=u;
    }
    
    if(v==-1)break;
    used[v]=1;
    ans+=mincost[v];
    
    for(int u=0;u<n;u++){
      mincost[u]=min(mincost[u],cost[v][u]);
    }
  }
  
  cout<<ans<<endl;
  
  return 0;
  
}