Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		matrix := make([][]int, 10)
		for i := range matrix {
			matrix[i] = make([]int, 10)
		}
		for i := range matrix {
			for j := range matrix[i] {
				if i == j {
					matrix[i][j] = 0
				} else {
					matrix[i][j] = INF
				}
			}
		}
		num := 0
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			from, _ := strconv.Atoi(buf[0])
			to, _ := strconv.Atoi(buf[1])
			cost, _ := strconv.Atoi(buf[2])
			if num < from {
				num = from
			}
			if num < to {
				num = to
			}
			matrix[from][to] = cost
			matrix[to][from] = cost
		}
		num++
		for i := 0; i < num; i++ {
			for j := 0; j < num; j++ {
				for k := 0; k < num; k++ {
					if matrix[j][i]+matrix[i][k] < matrix[j][k] {
						matrix[j][k] = matrix[j][i]+matrix[i][k]
					}
				}
			}
		}
		ans, sum := 0, INF
		for i := 0; i < num; i++ {
			now := 0
			for j := 0; j < num; j++ {
				now += matrix[i][j]
			}
			if sum > now {
				sum = now
				ans = i
			}
		}
		fmt.Println(ans, sum)
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
#define MAX 50
#define INF 9999999

int n,r[MAX][MAX],v[MAX],mini,mn,maxnum,minnum;
bool visit[MAX];

int dijkstra(int s,int g){
    for(int i = 0 ; i < MAX ; i++){
        v[i] = INF;
        visit[i] = false;
    }
    
    v[s] = 0;
    while(true){
        mini = INF;
        for(int i = 0 ; i <= maxnum ; i++){
            if(v[i] < mini && !visit[i]){
                mini = v[i];
                mn = i;
            }
        }
        
        if(mini == INF) break;
        visit[mn] = true;
        
        for(int i = 0; i <= maxnum ; i++){
            int t = r[mn][i]+v[mn];
            if(t < v[i]) v[i] = t;
        }
    }
    
    return v[g];
}


int main(){
    int a,b,c;
    
    while(cin >> n,n){
        for(int i = 0 ; i < MAX ; i++)
            for(int j = 0 ; j < MAX ; j++)
                r[i][j] = INF;
        
        maxnum = 0;
        minnum = INF;
        for(int i = 0 ; i < n ; i++){
            cin >> a >> b >> c;
            r[a][b] = c;
            r[b][a] = c;
            maxnum = max(maxnum,max(a,b));
            minnum = min(minnum,min(a,b));
        }
        
        int mx = INF, e;
        for(int i = 0; i <= maxnum ; i++){
            int sum = 0,num = i;
            for(int j = 0 ; j <= maxnum ; j++){
                if(i == j) continue;
                if(dijkstra(i,j)!=INF) sum += dijkstra(i,j);
            }
            if(sum < mx){
                mx = sum;
                e = num;
            }
        }
        cout << e << ' ' << mx << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <climits>
#include <queue>

#define INF (INT_MAX/3);

using namespace std;

class Edge{
    public:
        int to;
        int cost;
};

typedef pair<int, int> D_V;


int main(){
    int n;
    int a, b, c;
    while(true){
        cin >> n;
        if(n == 0) break;
        int max_n = 0;
        vector<Edge> edge[46];
        for(int i = 1; i <= n; i++){
            cin >> a >> b >> c;
            max_n = max(max_n, a);
            max_n = max(max_n, b);
            Edge e1, e2;
            e1.to = b;
            e1.cost = c;
            e2.to = a;
            e2.cost = c;
            edge[a].push_back(e1);
            edge[b].push_back(e2);
        }
        int d[46];
        int ans = INF;
        int ans_c;
        int sum;
        priority_queue<D_V, vector<D_V>, greater<D_V> > q;
        for(int s = 0; s <= max_n; s++){
            for(int i = 0; i <= max_n; i++){
                d[i] = INF;
            }
            d[s] = 0;
            
            q.push(D_V(d[s], s));
            
            while(!q.empty()){
                D_V d_v = q.top();
                q.pop();
                int v = d_v.second;

                if(d[v] < d_v.first) continue; 

                for(int i = 0; i < edge[v].size(); i++){
                    Edge e = edge[v][i];
                    if(d[e.to] > d[v] + e.cost){
                        d[e.to] = d[v] + e.cost;
                        q.push(D_V(d[e.to], e.to));
                    }
                }
            }
           
            sum = 0;
            for(int i = 0; i <= max_n; i++){
                sum += d[i];
            }
            if(ans > sum){
                ans = sum;
                ans_c = s;
            }
        }

        cout << ans_c << " " << ans << endl;
    }
    return 0;
}

C++: #include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<complex>

using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
typedef vector<int> vi;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))
#define INF (1<<28)
#define pb(i) push_back(i)
#define fs first
#define sc second

const char dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int d[512][512];
int main(){
	int n,m;
	while(cin >> n && n){
		m = 0;
		fill_n(d[0],512*512,INF);
		rep(i,n){
			int a,b,c;
			cin >> a >> b >> c;
			d[a][b] = d[b][a] = c;
			m = max(m,max(a+1,b+1));
		}
		rep(i,m)d[i][i] = 0;
		rep(k,m)rep(i,m)rep(j,m){
			d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
		}
		int city = -1, val = INF;
		rep(i,m){
			int tmp = 0;
			rep(j,m){
				tmp += d[i][j];
			}
			if(tmp < val){
				val = tmp;
				city = i;
			}
		}
		cout << city << " " << val << endl;
	}
	return 0;
}

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

#define MAX_V 10
#define MAX_E 45
#define INF 1<<28
int d[MAX_V][MAX_V];
int n;
int main() {
    while (cin >> n) {
        if (n == 0) break;
        for (int i = 0; i < MAX_V; i++) {
            for (int j = 0; j < MAX_V; j++) {
                if (i == j) {
                    d[i][j] = 0;
                } else {
                    d[i][j] = INF;
                }
            }
        }
        int biggest_c = 0;
        for (int i = 0; i < n; i++) {
            int from, to, weight;
            cin >> from >> to >> weight;
            d[from][to] = weight;
            d[to][from] = weight;
            if (biggest_c < to) biggest_c = to;
        }

//        for (int i = 0; i <= biggest_c; i++) {
//            for (int j = 0; j <= biggest_c; j++) {
//                cout << d[i][j] << ' ';
//            }
//            cout <<endl;
//        }

        for (int k = 0; k <= biggest_c; k++) {
            for (int i = 0; i <= biggest_c; i++) {
                for (int j = 0; j <= biggest_c; j++) {
                    if (d[i][j] > d[i][k] + d[k][j]) {
                        d[i][j] = d[i][k] + d[k][j];
                    }
                }
            }
        }

        int sums[biggest_c];
        for (int i = 0; i <= biggest_c; i++) {
            sums[i] = 0;
        }
        int min_sum = INF;
        int best_city = INF;
        for (int i = 0; i <= biggest_c; i++) {
            for (int j = 0; j <= biggest_c; j++) {
                sums[i] += d[i][j];
//                cout << d[i][j] << ' ';
            }
//            cout << endl;
        }
        for (int i = 0; i <= biggest_c; i++) {
            if (min_sum > sums[i]) {
                min_sum = sums[i];
                best_city = i;
            }
        }

        cout << best_city << ' ' << min_sum << endl;
    }
    return 0;
}