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;

constexpr int INF = 1e9;

int main() {
    int n;
    while(cin >> n, n) {
        vector<vector<int>> d(10, vector<int>(10, INF));
        for(int i=0; i<10; ++i) {
            d[i][i] = 0;
        }
        for(int i=0; i<n; ++i) {
            int a, b, c;
            cin >> a >> b >> c;
            d[a][b] = d[b][a] = c;
        }
        for(int k=0; k<10; ++k) {
            for(int i=0; i<10; ++i) {
                for(int j=0; j<10; ++j) {
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                }
            }
        }
        int cost = INF, town = -1;
        for(int i=0; i<10; ++i) {
            int c = 0;
            for(int j=0; j<10; ++j) {
                if(d[i][j] == INF) {
                    continue;
                }
                c += d[i][j];
            }
            if(c != 0 && cost > c) {
                cost = c;
                town = i;
            }
        }
        cout << town << ' ' << cost << endl;
    }
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
#define INF 1000000
int C[15][15];
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        for(int i=0;i<15;++i){
            for(int j=0;j<15;++j){
                C[i][j]=i==j?0:INF;
            }
        }
        int m=0;
        for(int i=0;i<n;++i){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            m=max(m,max(a,b));
            C[a][b]=C[b][a]=c;
        }
        m++;
        for(int k=0;k<m;++k){
            for(int i=0;i<m;++i){
                for(int j=0;j<m;++j){
                    C[i][j]=min(C[i][j],C[i][k]+C[k][j]);
                }
            }
        }
        int ln=INF,lc=INF;
        for(int i=m-1;i>=0;--i){
            int c=0;
            for(int j=0;j<m;++j){
                c+=C[i][j];
            }
            if(c<lc){
                ln=i;
                lc=c;
            }
        }
        printf("%d %d\n",ln,lc);
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 100000000;
using namespace std;

void warshallFloyd(int m[50][50], map<int, int> t){
    for(auto K = t.begin(); K != t.end(); K++){
        int k = K->first;
        for(auto I = t.begin(); I != t.end(); I++){
            int i = I->first;
            for(auto J = t.begin(); J != t.end(); J++){
                int j = J->first;
                m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
            }
        }
    }
}

int main(){
    int n;
    while(cin >> n, n){
        map<int, int> t;
        int m[50][50];
        int ans[2];
        ans[1] = INF;

        rep(i,50) rep(j,50) m[i][j] = INF;
        rep(i,50) m[i][i] = 0;
        rep(i,n){
            int a, b , c;
            cin >> a >> b >> c;
            m[a][b] = m[b][a] = c;
            t[a] = 1;
            t[b] = 1;
        }

        warshallFloyd(m, t);

        for(auto i = t.begin(); i != t.end(); i++){
            int sum = 0;
            for(auto j = t.begin(); j != t.end(); j++){
                sum+=m[i->first][j->first];
                //show(sum)
            }
            if(sum < ans[1] || (sum == ans[1] && ans[0] > i->first)){
                ans[0] = i->first;
                ans[1] = sum;
            }
        }
        cout << ans[0] << ' ' << ans[1] << endl;
    }
}

C++: #include <iostream>
using namespace std;
int dfs(int s[][300],int x,int y);
int max_int=999999;
int main()
{
    int n;
    while(cin>>n&&n)
    {
        int s[300][300]= {{0}},i,a,b,c,p=0,min,sum,k;
        for(i=0; i<n; i++)
        {
            cin>>a>>b>>c;
            s[a][b]=c;
            s[b][a]=c;
            if(b>p)
                p=b;
        }
        for(i=0; i<=p; i++)
        {
            sum=dfs(s,i,p);
            cout<<sum<<endl;
            if(i==0)
                min=sum,k=0;
            else if(sum<min)
                min=sum,k=i;
        }
        cout<<k<<" "<<min<<endl;
    }
    return 0;
}
int dfs(int s[][300],int x,int y)
{
    bool s0[300];
    int i,j,sum=0,dist[300]= {max_int};
    for(i=0; i<=y; ++i)
    {
        if(s[x][i])
            dist[i]=s[x][i];
        else
            dist[i]=max_int;
        s0[i]=0;
    }
    dist[x]=0;
    s0[x]=1;
    for(i=1; i<=y; ++i)
    {
        int tmp =max_int;
        int u = x;
        for(j=0; j<=y; ++j)
            if((!s0[j]) && dist[j]<tmp)
            {
                u = j;
                tmp = dist[j];
            }
        s0[u] = 1;
        for(j=0; j<=y; ++j)
            if((!s0[j]) && s[u][j]&& s[u][j]<max_int)
            {
                int newdist = dist[u] + s[u][j];
                if(newdist < dist[j])
                    dist[j] = newdist;
            }
    }
    for(i=0; i<=y; i++)
    {
        if(dist[i]<max_int)
            sum+=dist[i];
    }
    return sum;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<string.h>

using namespace std;

#define INF 1000000

int E,V,r[12][3],d[12][12];

void warshall_floyd(){
  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]);
}

void init(){
  for(int i=0;i<12;i++)
    for(int j=0;j<12;j++)
      d[i][j]=INF;

  memset(r,0,sizeof(r));
  V=0;
  E=0;
}

void solve(){
  warshall_floyd();

  int res[12];
  fill(res,res+12,0);


  for(int i=0;i<V;i++){
    for(int j=0;j<V;j++){
      if(d[i][j]!=INF)
        res[i]+=d[i][j];
    }
  }


  int res1=INF;
  int res2=INF;

  for(int i=0;i<V;i++){
    if(res[i]<res2){
      res1=i;
      res2=res[i];
    }
  }

  printf("%d %d\n",res1,res2);
}

int main(){
  while(true){
    init();
    scanf("%d",&E);
    if(E==0) break;
    for(int i=0;i<E;i++){
      scanf("%d%d%d",&r[i][0],&r[i][1],&r[i][2]);
    }

    for(int i=0;i<10;i++){
      V=max(V,r[i][1]);
    }
    V++;


    for(int i=0;i<E;i++){
      d[r[i][0]][r[i][1]]=r[i][2];
      d[r[i][1]][r[i][0]]=r[i][2];
    }

    for(int i=0;i<V;i++)
      d[i][i]=0;

    solve();
  }
  return 0;
}