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 <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <sstream>
#include <set>
#include <bitset>
#include <cmath>
using namespace std;

const int maxv = 10 + 5, INF = 0x3f3f3f3f;

int n, d[maxv][maxv];

int main() {
    while (cin >> n && n) {
        for (int i = 0; i < maxv; ++i)
            for (int j = 0; j < maxv; ++j)
                d[i][j] = (i == j ? 0 : INF);
        int V = 0;
        for (int i = 1; i <= n; ++i) {
            int a, b, c; cin >> a >> b >> c;
            d[a][b] = d[b][a] = c;
            V = max(V, max(a, b));
        }
        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]);
        int ans = INF, loc;
        for (int i = 0; i <= V; ++i) {
            int sum = 0;
            for (int j = 0; j <= V; ++j) {
                if (i == j) continue;
                else sum += d[i][j];
            }
            if (sum < ans) { ans = sum; loc = i; }
        }
        cout << loc << ' ' << ans << endl;
    }
    return 0;
}

C++: #include<stdio.h>


#define rep( i ,end ) for( int i = 0 ; i < end ; ++i )

#define CityN 10
#define INF 1000000

void Init( int NODE[ CityN ][ CityN ] ,int N )
{
	rep( i ,N )
	{
		rep( j ,N )
		{
			NODE[ i ][ j ] = ( i == j ) ? 0 : INF ;
		}
	}
}

int min( int a ,int b )
{
	return a > b ? b : a ;
}

int max( int a ,int b )
{
	return a > b ? a : b ;
}


int Node[ CityN ][ CityN ] ;

int main( void )
{
	int N ,n ,a ,b ,cost ;
	do
	{
		scanf( "%d" ,&N ) ;

		if( N == 0 )
		{
			return 0 ;
		}

		n = 0 ;
		Init( Node ,CityN ) ;

		rep( i ,N )
		{
			scanf( "%d %d %d" ,&a ,&b ,&cost ) ;
			
			Node[ a ][ b ] = Node[ b ][ a ] = cost ;

			n = max( n ,max( a ,b ) ) ;
		}

		N = n + 1 ;

		rep( k ,N )
		{
			rep( i ,N )
			{
				rep( j ,N )
				{
					Node[ i ][ j ] = min( Node[ i ][ j ] ,Node[ i ][ k ] + Node[ k ][ j ] ) ;
				}
			}
		}

		int Min = INF ,V ;
		rep( i ,N )
		{
			int sum = 0 ;

			rep( j ,N )
			{
				sum += Node[ i ][ j ] ;
			}

			if( Min > sum )
			{
				V = i ;
				
				Min = sum ;
			}
		}

		printf( "%d %d\n" ,V ,Min ) ;

	}while( 1 ) ;
}

C++: //2013/11/15(Fri)

#include <iostream>
using namespace std;
int main()
{
	
	while(true){
		int n;
		int a,b,c;
		int INF = 12001;
		int time[10][10];
		int m=0;
		int min=12000,imin=0;
		cin >> n;
		if(n==0)
			break;
		for(int i=0; i<10; i++)
			for(int j=0; j<10; j++){
				if(i==j)
					time[i][j] = 0;
		
				else
					time[i][j] = INF;
			}

		for(int i=0; i<n; i++){
			cin >> a >> b >> c;
			time[a][b] = c;
			time[b][a] = c;
			if(m<a)
				m=a;
			if(m<b)
				m=b;
		}


		for(int h=0; h<m+1; h++)
			for(int i=0; i<m+1; i++)
				for(int j=0; j<m+1; j++)
					if(time[i][h]==INF||time[h][j]==INF)
                        continue;
                    else if(time[i][j]==INF)
                        time[i][j] = time[i][h]+time[h][j];
                    else if(time[i][j]>time[i][h]+time[h][j])
                        time[i][j] = time[i][h]+time[h][j];

									
		
		for(int i=1; i<m+1; i++)
			min += time[0][i];


		for(int i=1; i<m+1; i++){
            int sum = 0;
            for(int j=0; j<m+1; j++)
                sum += time[i][j];
            if(sum<min){
                imin = i;
                min = sum;
            }
        }

		cout << imin << ' ' << min << endl;
	}
	return 0;
	
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
//#include <map>

using namespace std;

struct edge{
	int to,cost;
};
typedef long long ll;
typedef pair<ll,int> P;//firstがcost,secondがnodeを表す


vector<edge> G[20];//町から出る道を管理
int N;
ll d[20];
const ll INF=1000000000000;
bool flag=true;


void djikstra(int s){
	for(int i=0;i<N;i++){
		d[i]=INF;
	}
	
	priority_queue<P,vector<P>,greater<P> > que;//greater修正必要かも
	d[s]=0;
	//P np={0,s};
	que.push(P(0,s));
	
	while(que.size()){//確定したらそこから到達できるnodeをqueに登録する
		P p=que.top(); que.pop();
		int v=p.second;
		if(d[v]!=p.first) continue;//一度d[v]<p.totalになったらそのvはもう確定している
		for(int i=0;i<G[v].size();i++){
			edge e=G[v][i];
			if(d[v]+e.cost<d[e.to]){
				d[e.to]=d[v]+e.cost;
				//P np1={d[e.to],e.to};間違い
				que.push(P(d[e.to],e.to));
			}
		}
	}
}

void solve(){
	int R,ans;N=0;ll mind=INF;
	cin>>R;
	if(R==0) {flag=false; return;}
	for(int i=0;i<R;i++){
		int x,y,c;
		cin>>x>>y>>c;
		N=max(N,max(x,y));
		edge ne1={y,c},ne2={x,c};
		G[x].push_back(ne1);
		G[y].push_back(ne2);
	}
	N++;
	ll D[20]={0};
	for(int i=0;i<N;i++){
		djikstra(i);
		for(int j=0;j<N;j++){
			D[i]+=d[j];
		}
	}
	for(int i=0;i<N;i++){
		if(mind>D[i]){
			ans=i;
			mind=D[i];
		}
	}
	cout<<ans<<" "<<mind<<endl;
}

int main(){
	while(flag){
		solve();
	}
}

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

const int INF = 1000000000;

int main()
{
	while(true){
		int n;
		cin >> n;

		if(n==0)
			break;

		int T[10][10];
		for(int i=0; i<10; i++){
			for(int j=0; j<10; j++){
				if(i==j){
					T[i][j] = 0;
				}
				else{
					T[i][j] = INF;
				}
			}
		}

		int m = 0;		

		for(int i=0; i<n; i++){
			int a, b, c;
			cin >> a >> b >> c;

			T[a][b] = T[b][a] = c;

			if(a+1>m){
				m = a+1;
			}
			if(b+1>m){
				m = b+1;
			}
		}

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

	int town;
	int ti=INF;
	for(int i=m-1;i>=0;i--){
		int t=0;
		for(int j=0;j<m;j++){
			t+=T[i][j];
		}
		if(ti>=t){
			ti=t;
			town=i;
		}
	}
	cout<<town<<" "<<ti<<endl;
	

	}

	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#define INF 1000000

int edge[10][10];
int n;
bool isexist[10];

int calc(int city){
	int cost[10];
	std::fill_n(cost, 10, INF);
	cost[city]=0;

	while(true){
		bool update=false;
		for(int i=0;i<10;i++){
			if(!isexist[i]) break;
			for(int j=0;j<10;j++){
				if(!isexist[j]) break;
				if(cost[i]+edge[i][j]<cost[j]){
					cost[j]=cost[i]+edge[i][j];
					update=true;
				}
			}
		}
		if(!update) break;
	}
	int sum=0;
	for(int i=0;i<10;i++){
		if(isexist[i]) sum+=cost[i];
	}
	return sum;
}

void solve(){
	int min_city;
	int min_cost=INF;
	for(int i=0;i<10;i++){
		if(isexist[i]){
			int cost=calc(i);
			if(min_cost>cost){
				min_city=i;
				min_cost=cost;
			}
		}
	}
	std::printf("%d %d\n",min_city,min_cost);
	return;
}

int main(){
	while(std::scanf("%d",&n),n){
		for(int i=0;i<10;i++){
			isexist[i]=false;
			for(int j=0;j<10;j++){
				edge[i][j]=INF;
			}
		}
		for(int i=0;i<n;i++){
			int a,b,c;
			std::scanf("%d %d %d",&a,&b,&c);
			edge[a][b]=c;
			edge[b][a]=c;
			isexist[a]=true;
			isexist[b]=true;
		}
		solve();
	}

	return 0;
}