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 <cstdio>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int n, m, a, b, c, g[10][10], d[10][10], s[10];
int main() {
	while (true) {
		scanf("%d", &n);
		if (n == 0) break; m = 0;
		for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) g[i][j] = 99999999, d[i][j] = 999999999;
		for (int i = 0; i < n; i++) scanf("%d%d%d", &a, &b, &c), g[a][b] = c, g[b][a] = c, m = max({ a + 1, b + 1, m });
		for (int i = 0; i < m; i++) d[i][i] = 0;
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					d[j][k] = min(d[j][k], d[j][i] + g[i][k]);
				}
			}
		}
		int p = 0;
		for (int i = 0; i < m; i++) { s[i] = 0; for (int j = 0; j < m; j++) s[i] += d[i][j]; }
		for (int i = 0; i < m; i++) if (s[i] < s[p]) p = i;
		printf("%d %d\n", p, s[p]);
	}
	return 0;
}

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

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n;
  while (cin >> n, n) {
    const int INF = 1 << 30;
    int dis[10][10];
    fill_n((int*)dis, 10 * 10, INF);
    for (int i = 0; i < 10; i++) {
      dis[i][i] = 0;
    }
    int m = -1;
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      dis[a][b] = dis[b][a] = c;
      m = max(m, max(a, b) + 1);
    }
    for (int k = 0; k < m; k++) {
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          if (dis[i][k] == INF || dis[k][j] == INF) continue;
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
      }
    }
    int p = -1, q = 1 << 30;
    for (int i = 0; i < m; i++) {
      int sum = 0;
      for (int j = 0; j < m; j++) {
        sum += dis[i][j];
      }
      if (sum < q) p = i, q = sum;
    }
    cout << p << " " << q << endl;
  }

  return 0;
}

C++: #include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long int lnt;
typedef pair<lnt,lnt> P;
const lnt INF = 0xffffffffffffff;
lnt d[10][10];
lnt x,y,z;
lnt used[10];
int main()
{while(1){
	int n;
	cin>>n;
	if(!n) return 0;
	fill(d[0],d[10]+10,INF);
	fill(used,used+10,0);
	for(lnt i=0;i<n;i++){
		cin>>x>>y>>z;
		used[x]=1;
		used[y]=1;
		d[x][y]=z;
		d[y][x]=z;
	}
	for(lnt i=0;i<10;i++){
		if(used[i]) d[i][i]=0;
	}
	for(lnt k=0;k<10;k++){
		for(lnt i=0;i<10;i++){
			for(lnt j=0;j<10;j++){
				d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
			}
		}
	}
	P ans=P(-1,INF);
	lnt sum;
	for(lnt i=0;i<10;i++){
		if(!used[i]) continue;
		sum=0;
		for(lnt j=0;j<10;j++){
			if(!used[j]) continue;
			sum+=d[i][j];
		}
		if(ans.second>sum){
			ans.first=i;
			ans.second=sum;
		}
	}
	cout<<ans.first<<' '<<ans.second<<endl;
}}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+8
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = j; i < k; i++)
typedef long long ll;
typedef unsigned long long ull;

int main(){
	int n;
	while(scanf("%d",&n),n){
		int D[16][16] = {{0}};
		rep(i,16){
			rep(j,16){
				if(i != j)D[i][j] = INF;
			}
		}
		int v = 0;
		rep(i,n){
			int from,to,cost;
			scanf("%d%d%d",&from,&to,&cost);
			D[from][to] = cost;
			D[to][from] = cost;
			v = max(v,max(from,to));
		}
		v++;
		rep(k,v){
			rep(i,v){
				rep(j,v){
					D[i][j] = min(D[i][j],D[i][k]+D[k][j]);
				}
			}
		}
		int sum = INF,town = 0;
		rep(i,v){
			int tmp = 0;
			rep(j,v){
				if(D[i][j] == INF)tmp += 0;
				else tmp += D[i][j];
			}
			if(sum > tmp && tmp != 0){
				sum = tmp;
				town = i;
			}
		}
		printf("%d %d\n",town,sum);
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <algorithm>

const int kMaxTown = 10;
const int kInf = 1 << 16;

int cost[kMaxTown][kMaxTown];

int main(){

	while (1){
		int n;
		scanf(" %d", &n);
		if (n == 0) break;

		for (int i = 0; i < kMaxTown; i++)
			for (int j = 0; j < kMaxTown; j++)
				cost[i][j] = (i == j) ? 0 : kInf;

		for (int i = 0; i < n; i++){
			int a, b, c;
			scanf(" %d %d %d", &a, &b, &c);
			cost[a][b] = cost[b][a] = c;
		}

		for (int k = 0; k < kMaxTown; k++)
			for (int i = 0; i < kMaxTown; i++)
				for (int j = 0; j < kMaxTown; j++)
					cost[i][j] = std::min(cost[i][j], cost[i][k] + cost[k][j]);

		int best = 0;
		int bestSum = kInf;
		for (int i = 0; i < kMaxTown; i++){
			int sum = 0;
			for (int j = 0; j < kMaxTown; j++)
				if (cost[i][j] != kInf)
					sum += cost[i][j];
			if (sum == 0) continue;
			if (sum < bestSum){
				bestSum = sum;
				best = i;
			}
		}

		printf("%d %d\n", best, bestSum);
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,n) for(unsigned int i=0;i<(n);i++)
#define INF 1<<16
int main()
{
	while (true)
	{
		int A[10][10];
		REP(i, 10)REP(j, 10)
		{
			if (i == j)A[i][j] = 0;
			else A[i][j] = INF;
		}
		int N,M=-1;
		cin >> N;
		if (N == 0)break;
		REP(i, N)
		{
			int a, b, c;
			cin >> a >> b >> c;
			M = max(M, min(a, b));
			A[a][b] = c;
			A[b][a] = c;
		}
		REP(k, 10)REP(i, 10)REP(j, 10)
		{
			A[i][j] = min(A[i][j], A[i][k] + A[k][j]);
		}
		int min_value = INF;
		int min_index = -1;
		REP(i, M+1)
		{
			int sum = 0;
			REP(j, M+2)
			{
				sum += A[i][j];
			}
			if (min_value > sum)
			{
				min_index = i;
				min_value = sum;
			}
			else if (min_value == sum)
			{
				min_index = min(min_index, int(i));
			}
		}
		cout << min_index << " " << min_value << endl;
	}
	return 0;
}