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 <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1ll << 60;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

#ifdef WIN32
#define dump(x) cout << #x << " = " << (x) << "\n"
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n"
#else
#define dump(x)
#define debug(x)
#endif

struct edge {
	int to;
	int cost;
	edge( int t, int c ) {
		to = t;
		cost = c;
	}
};
int n;
int r[ 10 ][ 10 ];
vector<edge> e[ 10 ];

void dijkstra( int s ) {
	fill( r[ s ], r[ s ] + 10, INF );
	priority_queue< PII, vector<PII>, greater<PII> > que;
	que.push( PII( s, 0 ) );
	while ( que.size() ) {
		int to = que.top().first;
		int cost = que.top().second;
		que.pop();
		if ( r[ s ][ to ] < cost ) {
			continue;
		}
		r[ s ][ to ] = cost;
		for ( int i = 0; i < e[ to ].size(); i++ ) {
			if ( r[ s ][ e[ to ][ i ].to ] > cost + e[ to ][ i ].cost ) {
				r[ s ][ e[ to ][ i ].to ] = r[ e[ to ][ i ].to ][ s ] = cost + e[ to ][ i ].cost;
				que.push( PII( e[ to ][ i ].to, cost + e[ to ][ i ].cost ) );
			}
		}
	}
}

int main() {
#ifdef WIN32
	printf( "Debug Mode\n" );
#endif
	while ( ~scanf( "%d", &n ) && n ) {
		for ( int i = 0; i < 10; i++ ) {
			e[ i ].clear();
		}
		int m = 0;;
		for ( int i = 0; i < n; i++ ) {
			int s, g, c;
			scanf( "%d %d %d", &s, &g, &c );
			m = max( max( s, g ), m );
			dump( m );
			e[ s ].push_back( edge( g, c ) );
			e[ g ].push_back( edge( s, c ) );
		}
		for ( int i = 0; i < 10; i++ ) {
			if ( e[ i ].size() ) {
				m = i;
			}
			else {
				break;
			}
		}
		int ans_cost = INF;
		int ans_t = 0;
		for ( int i = 0; i <= m; i++ ) {
			dijkstra( i );
		}
		for ( int i = 0; i <= m; i++ ) {
			int cost = 0;
			for ( int j = 0; j <= m; j++ ) {
				cost += r[ i ][ j ];
			}
			if ( ans_cost > cost ) {
				ans_cost = cost;
				ans_t = i;
			}
		}
		printf( "%d %d\n", ans_t, ans_cost );
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#define N 11
#define inf 10000000
using namespace std;
int d[N][N],V;
void warshall_floyd(void)
{
	for(int k=0;k<=V;k++)
	{
		for(int i=0;i<=V;i++)
		{
			for(int j=0;j<=V;j++)
			d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
		}
	}
	return ;
}
void init(void)
{
	for(int i=0;i<N;i++)
	fill(d[i],d[i]+N,inf);
	for(int i=0;i<N;i++)
	d[i][i]=0;
	return ;
}
int main(void)
{
	int n;
	while(scanf("%d",&n)==1)
	{
		if(n==0)
		break;
		init();
		int s,e,c;
		V=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d%d",&s,&e,&c);
			d[s][e]=d[e][s]=c;
			V=max(V,max(s,e));
		}
		warshall_floyd();
		int ans=inf;
		int sum=0;
		int id=0;
		for(int i=0;i<=V;i++)
		{
			sum=0;
			for(int j=0;j<=V;j++)
			sum+=d[i][j];
			if(sum<ans)
			{
				ans=sum;
				id=i;
			}
		}
		printf("%d %d\n",id,ans);
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define INF 100000
using namespace std;

int main(){
	//int town[11][11];
	int to,from,cost;
	int n;
	while(1){
		cin>>n;
		if(!n)break;
		int maxsize=0;
		int dt[11][11];
		for(int i=0;i<11;i++){
			for(int j=0;j<11;j++){
				//town[i][j]=INF;
				if(i!=j)dt[i][j]=INF;
				else dt[i][j]=0;
				//out<<"yeah "<<i<<" "<<j<<endl;
			}
		}
		for(int i=0;i<n;i++){
			cin>>to>>from>>cost;
			//cout<<"analize"<<endl;
			dt[to][from]=cost;
			dt[from][to]=cost;
			maxsize=max(maxsize,to);
			maxsize=max(maxsize,from);
		}
		for(int k=0;k<maxsize+1;k++){
			for(int i=0;i<maxsize+1;i++){
				for(int j=0;j<maxsize+1;j++){
					dt[i][j]=min(dt[i][j],dt[i][k]+dt[k][j]);
				}
			}
		}
		int sum[maxsize+1];
		rep(i,maxsize+1){
			sum[i]=0;
			rep(j,maxsize+1){
				sum[i]+=dt[i][j];
			}
			//cout<<sum[i]<<" ";
		}
		//cout<<endl;
		int ans[maxsize+1];
		rep(i,maxsize+1)ans[i]=sum[i];
		sort(sum,sum+maxsize+1);
		//cout<<sum[0]<<endl;
		for(int i=0;i<maxsize+1;i++){
			if(ans[i]==sum[0]){
				cout<<i<<" "<<sum[0];
				break;
			}
		}
		cout<<endl;
		//cout<<"finish"<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define f first
#define s second
using namespace std;

int n;
typedef pair<int, int> P; //<cost, to>
vector<P> node[10];
priority_queue<P, vector<P>, greater<P> > que;

int dijkstra(int s, int city);

int main(){
  while(cin >> n, n){
    int city = -1;
    for(int i = 0; i < 10; i++) node[i].clear();
    for(int i = 0; i < n; i++){
      int a, b, c;
      cin >> a >> b >> c;
      node[a].push_back(P(c, b));
      node[b].push_back(P(c, a));
      city = max(city, a);
      city = max(city, b);
    }
    P mini = P(0, INT_MAX);
    for(int i = 0; i <= city; i++){
      int x = dijkstra(i, city);
      if(mini.s > x){
	mini.f = i;
	mini.s = x;
      }
    }

    cout << mini.f << " " << mini.s << endl;
  }
}

int dijkstra(int s, int city){
  int allcost = 0;
  for(int i = 0; i <= city; i++){
    que.push(P(0, s));
    int mincost = INT_MAX;
    bool moved[10][10] = {{false}};
    while(!que.empty()){
      P now = que.top(); que.pop();
      if(i == now.s) mincost = min(mincost, now.f);
      else{
	for(int j = 0; j < node[now.s].size(); j++){
	  P next = node[now.s][j];
	  if(!moved[now.s][next.s]){
	    moved[now.s][next.s] = true;
	    moved[next.s][now.s] = true;
	    next.f += now.f;
	    que.push(next);
	  }
	}
      }
    }
    allcost += mincost;
  }
  return allcost;
}