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 <algorithm>

using namespace std;

const int INF = 10000000;

int main(void)
{
	int m,n;
	int city[11][11];

	while( cin >> m && m ){
		for( int i = 0;i < 11;i++ ){
			for( int j = 0;j < 11;j++ ){
				city[i][j] = INF;
			}
		}

		for( int i = 0;i < 11;i++ ){
			city[i][i] = 0;
		}

		for( int i = 0;i < m;i++ ){
			int a,b,cost;

			cin >> a >> b >> cost;
			city[a][b] = cost;
			city[b][a] = cost;
		}

		for( int k = 0;k <= 10;k++ ){
			for( int j = 0;j <= 10;j++ ){
				for( int i = 0;i <= 10;i++ ){
					city[k][i] = min( city[k][i], city[k][j] + city[j][i] );
				}
			}
		}
		int answer = 0;
		int sum;
		int answer_sum = INF;
		bool pass;
		for( int i = 0;i <= 10;i++ ){
			sum = 0;
			pass = false;
			for( int j = 0;j <= 10;j++ ){
				sum += city[i][j];
				if( city[i][j] == INF ) sum -= INF;
				else if( i != j ) pass = true;
			}
			if( answer_sum > sum && pass ){
				answer_sum = sum;
				answer = i;
			}
		}


		cout << answer << " " << answer_sum << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int N, M;
int d[15][15];

bool read() {
  cin >> M;
  if (M == 0) return false;
  for (int i = 0; i <= 10; i++) {
    for (int j = 0; j <= 10; j++) {
      d[i][j] = i<<20;
    }
    d[i][i] = 0;
  }
  N = 0;
  int s, e, m;
  for (int i = 0; i < M; i++) {
    cin >> s >> e >> m;
    d[s][e] = m;
    d[e][s] = m;
    N = max(N, s);
    N = max(N, e);
  }
  return true;
}

void floyd(int n) {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

void solve() {
  floyd(N+1);
  int dmin = 1<<20, idx = 0;
  for (int i = 0; i <= N; i++) {
    int dis = 0;
    for (int j = 0; j <= N; j++) dis += d[i][j];
    if (dis < dmin) dmin = dis, idx = i;
  }
  cout << idx << ' ' << dmin;
}


int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (read()) {
    solve();
  }
  return 0;
}

C++: # include<iostream>
#include<vector>
#include<queue>
#include<functional>
#define INF 2e6

using P = std::pair < int, int > ;

# define MAX_E 45
# define MAX_V 10

int V, E;

struct edge{
	int to, cost;
	edge() = default;
	edge(const edge &)= default;
	edge& operator = (const edge&) = default;
	edge(int _t, int _c) : to(_t), cost(_c) {}
};

std::vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s){
	std::priority_queue<P, std::vector<P>, std::greater<P>> Q;
	std::fill(d, d + V+1, INF);
	d[s] = 0;
	Q.push(P(0, s));
	while (!Q.empty()){
		P p = Q.top(); Q.pop();
		int v = p.second;
		if (d[v] < p.first)continue;
		for (int i = 0; i < G[v].size(); ++i){
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost){
				d[e.to] = d[v] + e.cost;
				Q.push(P(d[e.to], e.to));
			}
		}
	}
}

P solve(){
	int min = INF, minv;
	for (int i = 0; i <= V; ++i){
		int sum = 0;
		dijkstra(i);
		for (int j = 0; j <= V; ++j){
			sum += d[j];
		}
		if (min > sum)min = sum, minv = i;
	}
	return P(minv, min);
}

int main(){
	while (1){
		std::cin >> E;
		if (E == 0)
			break;
		for (int i = 0; i <= V; i++)G[i].clear();
		V = 0;
		int f, t, c;
		for (int i = 0; i < E; ++i){
			std::cin >> f >> t >> c;
			G[f].push_back(edge(t, c));
			G[t].push_back(edge(f, c));
			if (V < t)
				V = t;
		}
		std::cout << solve().first<<' '<< solve().second << std::endl;
	}
	return 0;
}

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

#define rep(i,n) for(int i=0; i<n; i++)
typedef long long ll;
const int INF = 1<<20;

int main() {
	int n;
	while(cin >> n, n) {
		int dp[10][10];
		bool exist[10] = {};
		rep(i, 10) rep(j, 10) dp[i][j] = INF;
		rep(i, 10) dp[i][i] = 0;
		rep(i, n) {
			int a, b, c;
			cin >> a >> b >> c;
			dp[a][b] = dp[b][a] = c;
			exist[a] = exist[b] = true;
		}
		rep(k, 10) {
			rep(i, 10) {
				rep(j, 10) {
					dp[i][j] = min( dp[i][j], dp[i][k]+dp[k][j] );
				}
			}
		}
		int mini = INF, res = INF;
		rep(i, 10) {
			if(!exist[i]) continue;
			int sum = 0;
			rep(j, 10) {
				if(!exist[j]) continue;
				sum += dp[i][j];
			}
			if( sum < mini ) {
				mini = sum;
				res = i;
			}
		}
		cout << res << ' ' << mini << endl;
	}
	return 0;
}

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 time=INF;
	for(int i=0;i<m;i++){
		int t=0;
		for(int j=0;j<m;j++){
			t+=T[i][j];
		}
			if(time>=t){
			time=t;
			town=i;
	}
}
	cout<<town<<" "<<time<<endl;
	

	}

	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <climits>
typedef long long ll;

const int max_t = 20;
ll dist[max_t][max_t];

int main() {
	//freopen("aoj0189.txt", "r", stdin);
	while (1) {
		int n, t = 0;
		scanf("%d", &n);
		if (n == 0) break;
		
		for (int i = 0; i < max_t; i++) {
			std::fill(dist[i], dist[i] + max_t, INT_MAX);
		}
		for (int i = 0; i < n; i++) {
			int from, to;
			ll d;
			scanf("%d%d%lld", &from, &to, &d);
			t = std::max(t, from);
			t = std::max(t, to);
			dist[from][to] = d;
			dist[to][from] = d;
		}
		for (int i = 0; i <= t; i++) {
			dist[i][i] = 0;
		}

		for (int k = 0; k <= t; k++) {
		for (int i = 0; i <= t; i++) {
			for (int j = 0; j <= t; j++) {
						dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);
				}
			}
		}

		ll min_len = INT_MAX;
		int start = -1;
		for (int i = 0; i <= t; i++) {
			ll len = 0;
			for (int j = 0; j <= t; j++) {
				if (j == i) continue;

				len += dist[i][j];
			}
			if (len < min_len) {
				min_len = len;
				start = i;
			}
		}

		printf("%d %lld\n", start, min_len);
	}
	return 0;
}