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 <vector>
#include <queue>

const int INF = INT_MAX/10;

using namespace std;

const int V_MAX = 10;
const int E_MAX = 45;


typedef pair<int, int> P; // v, cost

int main() {
  int N;
  while (cin >> N) {
    int V = 0;
    int d[V_MAX][V_MAX];
    if (N == 0) break;
    for (int i = 0; i < V_MAX; i++) {
      for (int j = 0; j < V_MAX; j++) {
        if (i == j) d[i][j] = 0;
        else d[i][j] = INF;
      }
    }
    for (int i = 0; i < N; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      d[a][b] = c;
      d[b][a] = c;
      V = max(V, a); V = max(V, b);
    }
    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 m = INF;
    int ans = -1;
    for (int i = 0; i <= V; i++) {
      int sum = 0;
      for (int j = 0; j <= V; j++) {
        if (i != j) {
          if (d[i][j] != INF) {
            sum += d[i][j];
          }
        }
      }
      if (sum < m) {
        m = sum;
        ans = i;
      }
    }
    printf("%d %d\n", ans, m);
  }

  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define MAX 0x3f3f3f3f
using namespace std;

int mpa[11][11];

int n,flag=0;

void floyd()
{
    for(int k=0; k<=9; k++)
    {
        for(int i=0; i<=9; i++)
        {
            for(int j=0; j<=9; j++)
            {
                if(mpa[i][j]>mpa[i][k]+mpa[k][j])
                    mpa[i][j]=mpa[i][k]+mpa[k][j];
            }
        }
    }
}


int main()
{
    while(cin>>n,n!=0)
    {
        flag=0;
        memset(mpa,MAX,sizeof(mpa));
        for(int i = 1; i <= n; i++)
        {
            int a, b, c;
            cin>>a>>b>>c;

            if(mpa[a][b]>c)
            {
                mpa[a][b]=c;
                mpa[b][a]=c;
            }
        }

        floyd();

        for(int i=0; i<10; i++)
            mpa[i][10]=0;

        int  mmin=MAX,a1,a2,flag=0;
        for(int i=0; i<10; i++)
        {
            for(int j=0 ; j<10; j++)
            {
                if(i == j)
                    mpa[i][j]=0;
                if(mpa[i][j]!=MAX)
                    mpa[i][10]+=mpa[i][j];
            }
            if(mmin>mpa[i][10]&& mpa[i][10]!=0)
            {
                mmin=mpa[i][10];
                a1=i;
            }
            else if(mmin==mpa[i][10] && mpa[i][10]!=0)
            {
                flag=1;
                a2=min(a1,a2);
            }
        }

        if(flag==1)
        {

            cout<<a2<<" "<<mpa[a1][10]<<endl;
        }
        else
        {
            cout<<a1<<" "<<mpa[a1][10]<<endl;
        }

    }
    return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int edge[50][50];

int main(){
	int n;
	while(cin>>n){
		if(n==0)break;

		rep(i,50)rep(j,50){
			if(i==j)edge[i][j]=0;
			else edge[i][j]=99999999;
		}

		int m=-1;
		rep(i,n){
			int a,b,c;
			cin>>a>>b>>c;
			m=max( m , max(a,b) );
			edge[a][b]=edge[b][a]=c;
		}
		m++;
		rep(k,m){
			rep(i,m){
				rep(j,m){
					edge[i][j]=min( edge[i][j] , edge[i][k]+edge[k][j] );
				}
			}
		}
		int mini=9999999;
		int num;
		rep(i,m){
			int sum=0;
			rep(j,m){
				sum+=edge[i][j];
			}
			if(mini>sum){
				mini=sum;
				num=i;
			}
		}
		cout<<num<<" "<<mini<<endl;
	}
	return 0;
}

C++: #include<iostream>

#define TOWN_MAX 11
#define ROAD_MAX 46
#define INF 10000000000000000

int solve(int);

int main(int argc, char const* argv[]) {
    int n;

    while (true) {
        std::cin >> n;
        if (n == 0) break;
        solve(n);
    }
    return 0;
}

int solve(int road_num) {
    long long distances[TOWN_MAX][TOWN_MAX];
    bool used[TOWN_MAX];

    for (int i = 0; i < TOWN_MAX; i++) {
        used[i] = false;
        for (int j = 0; j < TOWN_MAX; j++)
            distances[i][j] = (i == j) ? 0 :INF;
    }

    for (int i = 0; i < road_num; i++) {
        int src, dest, cost;
        std::cin >> src >> dest >> cost;
        distances[src][dest] = cost;
        distances[dest][src] = cost;
        used[src] = used[dest] = true;
    }

    for (int infix_road = 0; infix_road < road_num; infix_road++) {
        for (int begin_town = 0; begin_town < TOWN_MAX; begin_town++) {
            for (int end_town = 0; end_town < TOWN_MAX; end_town++) {
                distances[begin_town][end_town] = std::min(distances[begin_town][end_town],
                        distances[begin_town][infix_road] + distances[infix_road][end_town]);
            }
        }
    }

    int min_cost       = INF;
    int min_cost_index = 0;
    for (int i = 0; i < TOWN_MAX; i++) {
        if (!used[i]) continue;

        int sum_cost   = 0;
        for (int j = 0; j < TOWN_MAX; j++) {
            if (!used[j]) continue;
            sum_cost += distances[i][j];
        }

        if (sum_cost < min_cost) {
            min_cost       = sum_cost;
            min_cost_index = i;
        }
    }

    std::cout << min_cost_index << " " << min_cost << std::endl;

    return 0;
}