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 <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <cstring>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
int wf[10][10];
int main(){
    for(int e; cin >> e;){
        if(e == 0) return 0;
        rep(i, 10) rep(j, 10)
          if(i == j) wf[i][j] = 0; else wf[i][j] = INF;
        set <int> town;
        rep(i, e){
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            wf[x][y] = wf[y][x] = z;
            town.insert(x); town.insert(y);
        }
        int n = town.size();
        rep(k, n) rep(i, n) rep(j, n)
          wf[i][j] = min(wf[i][j], wf[i][k] + wf[j][k]);
        int nans = 0, tans = INF;
        rep(i, n){
            int temp = 0;
            rep(j, n) temp += wf[i][j];
            if(tans > temp){
                tans = temp;
                nans = i;
            }
        }
        cout << nans << " " << tans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
int cost[10][10];
#define INF 10e6

using namespace std;

int main(void){
	int n,a,b,d;
	int cmax;
	int t,rmin,tmin;
	while(cin >> n,n){
		tmin=INF;
		cmax=0;
		for(int i=0;i<10;i++)
				for(int j=0;j<10;j++)
					cost[i][j]=INF;
					
		for(int i=0;i<10;i++)
			cost[i][i]=0;
			
		for(int i=0;i<n;i++){
			cin >> a >> b >> d;
			if(cmax<a)
				cmax=a;
			if(cmax<b)
				cmax=b;
			cost[a][b]=cost[b][a]=d;
		}
		for(int i=0;i<=cmax;i++)
			for(int j=0;j<=cmax;j++)
				for(int k=0;k<=cmax;k++)
					if(cost[i][j]>cost[i][k]+cost[k][j])
						cost[i][j]=cost[i][k]+cost[k][j];
		
		for(int i=0;i<=cmax;i++){
			t=0;
			for(int j=0;j<=cmax;j++)
					t+=cost[i][j];
			if(t<tmin)
				tmin=t,rmin=i;
		}
		cout << rmin << ' ' << tmin << endl;
	}
	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]<<" ";
		}
		int ans[maxsize];
		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];
			}
		}
		cout<<endl;
		//cout<<"finish"<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define inf 1000000000
using namespace std;
unsigned long long d[10][10],sum[10];
int v;

int main()
{
	int n;
	while(cin>>n,n)
	{
		bool used[10];
		memset(used,0,sizeof(used));
		memset(d,0,sizeof(d));
		memset(sum,0,sizeof(sum));
		for(int i=0;i<n;i++)
		{
			int s,t,c;
			cin>>s>>t>>c;
			used[s]=used[t]=true;
			d[s][t]=d[t][s]=c;
		}
		v=0;
		for(int i=0;i<10;i++)
		{
			if(used[i]) v++;
			for(int j=0;j<10;j++)
			{
				if(d[i][j]==0&&i!=j) d[i][j]=inf;
			}
		}

		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;
		for(int i=0;i<v;i++)
		{
			for(int j=0;j<v;j++)
			{
				sum[i]+=d[i][j];
			}
		}
		ans=min_element(sum,sum+v)-sum;
		cout<<ans<<" "<<sum[ans]<<endl;

	}
	return 0;
}

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

const int INF = 100000;

int main(){
	while(true){
		int n;
		cin>>n;
		if(n==0)break;
		int a[n],b[n],c[n];
		int N = 0;
		for(int i=0;i<n;++i){
			cin>>a[i]>>b[i]>>c[i];
			if(N<a[i]+1)N = a[i]+1;
			if(N<b[i]+1)N = b[i]+1;
		}
		vector<vector<int> > DParr;
		vector<int>tmparr(N,INF);
		for(int i=0;i<N;++i){
			DParr.push_back(tmparr);
			DParr[i][i] = 0;
		}
		for(int i=0;i<n;++i){
			DParr[a[i]][b[i]] = c[i];
			DParr[b[i]][a[i]] = c[i];
		}
		for(int k=0;k<N;++k){
			for(int i=0;i<N;++i){
				for(int j=0;j<N;++j){
					DParr[i][j] = min(DParr[i][j], DParr[i][k] + DParr[k][j]);
				}
			}
		}
		/*
		for(int i=0;i<N;++i){
			for(int j=0;j<N;++j){
				cout<<DParr[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;
		*/
		int ans=100000;
		int city;
		for(int i=0;i<N;++i){
			int tmpans=0;
			for(int j=0;j<N;++j){
				tmpans+=DParr[i][j];
			}
			if(ans>tmpans){
				ans=tmpans;
				city = i;
			}
		}
		cout<<city<<" "<<ans<<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

#define INF 1e6

struct WarshallFloyd {
  vector<vector<int>> d;
  int v;

  WarshallFloyd(int n) : v(n), d(n, vector<int>(n, INF)) {
    for(int i = 0; i < v; ++i) d[i][i] = 0;
  }

  void add(int from, int to, int cost) {
    d[from][to] = cost;
    d[to][from] = cost;
  }

  void make() {
    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 dist(int i, int j) {
    return d[i][j];
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  while(cin >> n, n != 0) {
    WarshallFloyd wf(10);
    for(int i = 0; i < n; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      wf.add(a, b, c);
    }
    wf.make();

    int num = 0;
    while(wf.dist(0, num) != INF) ++num;

    pair<int, int> ans(INF, -1);
    for(int i = 0; i < num; ++i) {
      int sum = 0;
      for(int j = 0; j < num; ++j) {
        sum += wf.dist(i, j);
      }
      ans = min(ans, {sum, i});
    }
    cout << ans.second << " " << ans.first << endl;
  }
}