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 <queue>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define INF 9999999

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;


bool shortestPath(const Graph &g,
    Matrix &dist, vector<vector<int> > &prev) {
  int n = g.size();
  Array h(n+1);
  REP(k,n) REP(i,n) FOR(e,g[i]) {
    if (h[e->dst] > h[e->src] + e->weight) {
      h[e->dst] = h[e->src] + e->weight;
      if (k == n-1) return false; // negative cycle
    }
  }
  dist.assign(n, Array(n, INF));
  prev.assign(n, vector<int>(n, -2));
  REP(s, n) {
    priority_queue<Edge> Q;
    Q.push(Edge(s, s, 0));
    while (!Q.empty()) {
      Edge e = Q.top(); Q.pop();
      if (prev[s][e.dst] != -2) continue;
      prev[s][e.dst] = e.src;
      FOR(f,g[e.dst]) {
        if (dist[s][f->dst] > e.weight + f->weight) {
          dist[s][f->dst] = e.weight + f->weight;
          Q.push(Edge(f->src, f->dst, e.weight + f->weight));
        }
      }
    }
    REP(u, n) dist[s][u] += h[u] - h[s];
  }
}
vector<int> buildPath(const vector< vector<int> >& prev, int s, int t) {
  vector<int> path;
  for (int u = t; u >= 0; u = prev[s][u])
    path.push_back(u);
  reverse(ALL(path));
  return path;
}

int main() {
	int n;
	while(cin>>n,n){
		Graph g(10);
		Matrix d;
		vector<vector<int> > p;
		REP(i,n){
			int a,b,c;
			cin>>a>>b>>c;
			g[a].push_back(Edge(a,b,c));
			g[b].push_back(Edge(b,a,c));
		}
		shortestPath(g,d,p);

		int mn=INF,t,mi;
		REP(i,d.size()){
			if(d[i][0]==INF)continue;
			t=0;
			REP(j,d[i].size()){
				if(d[i][j]==INF||i==j)continue;
				t+=d[i][j];
			}
			if(t<mn)mn=t,mi=i;
		}
		cout<<mi<<" "<<mn<<endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
const int INF = 10000;

int n;
int d[10];
int D[10];

struct edge {
    int u, v, cost;
    edge() {}
    edge(int uu, int vv, int cc) : 
         u(uu), v(vv), cost(cc) {}
};

edge edges[45];

int main() {
    while (cin>>n) {
    if (n == 0) break;
    int a, b, c;
    int m = 0;
    for (int i = 0; i < n; i++) {
        cin >> a; cin >> b; cin >> c;
        edges[i].u = a;
        edges[i].v = b;
        m = max(a,m); m = max(b,m);
        edges[i].cost = c;
    }
    m++;
    fill(D,D+m,0);
    int res = 0;
    //m 町の数 n道の数
    for (int k = 0; k < m; k++) {
        fill(d,d+m,INF);
        d[k] = 0;
        bool updated = true;
        while (updated) {
            updated = false;
            for (int i = 0; i < n; i++) {
                edge e = edges[i];
                if (d[e.u] != INF && d[e.u] + e.cost < d[e.v]) {
                    updated = true;
                    d[e.v] = d[e.u] + e.cost;
                }
                swap(e.u,e.v);
                if (d[e.u] != INF && d[e.u] + e.cost < d[e.v]) {
                    updated = true;
                    d[e.v] = d[e.u] + e.cost;
                }  
            }
        }
        for (int i = 0; i < m; i++) {
            D[k] += d[i];
        }
    }
    int mm = 0, md = INF;
    for (int i = 0; i < m; i++) {
        if (D[i] < md) {
            mm = i;
            md = D[i];
        }
    }
    cout << mm << " " << md << "\n";
    }
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 10000000
int route[10][10],d[10],n,sum;
int min(int a,int b){
	if(a<b){
		return a;
	}else{
		return b;
	}
}
void dijkstra(int a){
	int s[10],k,num,i,j;
	sum=0;
	for(i=0;i<10;i++){
		s[i]=0;
		d[i]=INF;
	}
	d[a]=0;
	for(i=0;i<10;i++){
		k=INF;
		for(j=0;j<10;j++){
			if(s[j]==0){
				if(k>d[j]){
					k=d[j];num=j;
				}
			}
		}
		s[num]=1;
		for(j=0;j<10;j++){
			if(route[num][j]!=INF){
				d[j]=min(d[j],d[num]+route[num][j]);
			}
		}
	}
	
	for(i=0;i<10;i++){
		if(s[i]==1){
			sum+=d[i];
		}
	}
	if(sum==0)sum=INF;
	return;

}

int main(){
	while(1){
		int i,j,a,b,c,ans,min=INF;
		cin>>n;
		if(n==0)break;
		for(i=0;i<10;i++){
			for(j=0;j<10;j++){
				route[i][j]=INF;
			}
		}
		for(i=0;i<n;i++){
			cin>>a>>b>>c;
			route[a][b]=c;
			route[b][a]=c;
		}
		for(i=0;i<10;i++){
			dijkstra(i);
			if(sum<min){
				min=sum;ans=i;
			}
		}
		cout<<ans<<' '<<min<<endl;
	}
    return 0;
}

C++: #include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int MAXN = 100000;
const int INF = 1e9 + 5;

int dist[10][10];

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int M;
	while (scanf("%d", &M) == 1 && M) {
		for (int i = 0; i < 10; i++) {
			fill(dist[i], dist[i] + 10, INF);
			dist[i][i] = 0;
		}
		
		for (int i = 0; i < M; i++) {
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			dist[a][b] = dist[b][a] = min(dist[b][a], c);
		}
		
		for (int k = 0; k < 10; k++) {
			for (int i = 0; i < 10; i++) {
				for (int j = 0; j < 10; j++) {
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
				}
			}
		}
		
		int id = -1, ans = INF;
		for (int i = 0; i < 10; i++) {
			int res = 0;
			for (int j = 0; j < 10; j++) {
				if (dist[i][j] != INF) {
					res += dist[i][j];
				}
			}
			if (res != 0 && res < ans) {
				ans = res;
				id = i;
			}
		}
		
		printf("%d %d\n", id, ans);
	}
	
	/**** Note MAXN ****/
}