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<algorithm>
#include<cassert>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<iomanip>
#include<map>
#include<numeric>
#include<queue>
#include<vector>
#include<set>
#include<string>
#include<stack>
#include<sstream>
#include<complex>

#define pb push_back
#define clr clear()
#define sz size()
#define fs first
#define sc second

#define rep(i,a) for(int i=0;i<(int)(a);i++)
#define rrep(i,a) for(int i=(int)(a)−1;i>=0;i−−)
#define all(a) (a).begin(),(a).end()
#define EQ(a,b) (abs((a)−(b)) < EPS)
#define INIT(a) memset(a,0,sizeof(a))

using namespace std;
typedef double D;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const D EPS = 1e-8;
const int INF = 1e8;
const D PI = acos(-1);

struct edge{
  int to,cost;
  bool operator<(const edge x)const{return cost>x.cost; }
};
int v; //頂点数
int d[20]; //s から i への最短路
vector<edge> G[20]; //ク&#12441;ラフの隣接リスト表現

void AddEdge(int s,int g,int p){
  G[s].pb((edge){g,p});
}


void BellmanFord(int s){
  fill(d,d+v,INF);
  d[s] = 0;
  for(;;){
    bool f = false; rep(i,v)rep(j,G[i].sz){
      edge e = G[i][j];
      if(d[i]<INF && d[e.to] > d[i] + e.cost){
	d[e.to] = d[i] + e.cost;
	f = true;
      }
    }
    if(!f)break;
  }
}

int n;
int a,b,c;

int main(){
  while(cin >> n,n){
    rep(i,20){
      G[i].clear();
    }

    v = 0;
    rep(i,n){
      cin >> a >> b >> c;
      v = max(v,max(a,b));
      AddEdge(a,b,c);
      AddEdge(b,a,c);
    }
    v++;

    int res = -1, dis = INF;
    rep(i,v){
      int tmp = 0;
      BellmanFord(i);
      rep(j,v)tmp += d[j];
      if(dis > tmp){
	dis = tmp; res = i;
      }
    }
    cout << res << " " << dis <<  endl;
  }
    
}

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

using namespace std;

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

vector<edge> G[20];//町から出る道を管理
int N;
int d[20];
const int INF=100000000;
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,mind=INF,ans;N=0;
	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++;
	int 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 <cstdio>
#include <algorithm>

using namespace std;

int map[10][10];

int cmp(int i,int j,int k){
  if(map[i][j]==-1){
    if(map[i][k]>=0 && map[k][j]>=0)
      return map[i][k]+map[k][j];
    else
      return -1;
  }else
    if(map[i][k]==-1 || map[k][j]==-1)
      return map[i][j];
    else
      return min(map[i][j],map[i][k]+map[k][j]);
}

int main(){
  for(int n;scanf("%d",&n),n;){
    int m=0;
    memset(map,-1,sizeof(map));

    for(;n--;){
      int a,b,c;
      scanf("%d %d %d",&a,&b,&c);
      m=m>a?m:a;m=n>b?m:b;
      map[a][b] = map[b][a] = c;
    }

    for(int i=0; i<=m; i++)
      map[i][i] = 0;

    for(int k=0; k<=m; k++)
      for(int i=0; i<=m; i++)
	for(int j=0; j<=m; j++)
	  map[i][j] = cmp(i,j,k);

    // for(int i=0; i<=m; i++){
    //   for(int j=0; j<=m; j++)
    // 	printf("%3d ",map[i][j]);
    //   putchar('\n');
    // }

    int num,sum=-1;
    for(int i=0; i<=m; i++){
      int tsum=0;
      for(int j=0; j<=m; j++)
	tsum += map[i][j];
      if(sum==-1||tsum<sum){
	num=i; sum=tsum;
      }
    }

    printf("%d %d\n",num,sum);
  }
}

C++: #include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
//aoj 0189
using namespace std;
int n;
struct pt{
	int e;
	int d;
};
int mbh,mans;
int ans;
int bh[11];
vector<pt> cun[21];
int vis[21];
int dis[21];
queue<int> dl;
void spfa(int ji)
{
	memset(dis,127,sizeof(dis));
	memset(vis,0,sizeof(vis));
	while(!dl.empty())
	{
		dl.pop();
	}
	dis[ji]=0;
	vis[ji]=1;
	dl.push(ji);
	while(!dl.empty())
	{
		int ls=dl.front();
		dl.pop();
		for(int i=0;i<cun[ls].size();i++)
		{
			int ee=cun[ls][i].e;
			int dd=cun[ls][i].d;
			if(dis[ee]>dis[ls]+dd)
			{
				dis[ee]=dis[ls]+dd;
				if(vis[ee]==0)
				{
					vis[ee]=1;
					dl.push(ee);
				}
			}
		}
		vis[ls]=0;
	}
	return;
}
int main()
{
	while(1)
	{
		scanf("%d",&n);
		if(n==0)
		{
			break;
		}
		mans=271270;
		ans=0;
		memset(bh,0,sizeof(bh));
		for(int i=0;i<=9;i++)
		{
			cun[i].clear();
		}
		for(int i=1;i<=n;i++)
		{
			int s,e;
			pt ls;
			scanf("%d%d%d",&s,&e,&ls.d);
			bh[s]=1;
			bh[e]=1;
			ls.e=e;
			cun[s].push_back(ls);
			ls.e=s;
			cun[e].push_back(ls);
		}
		for(int i=0;i<=9;i++)
		{
			if(bh[i]!=0)
			{
				spfa(i);
				for(int j=0;j<=9;j++)
				{
					if(dis[j]<20000)
					{
						ans+=dis[j];
					}
				}
				if(ans<mans)
				{
					mans=ans;
					mbh=i;
				}
			}
			ans=0;
		}
		printf("%d %d\n",mbh,mans);
	}
	return 0;
}