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>
#define INFINITY (1 << 30)

#define MAX_N 10

using namespace std;

int n,road[MAX_N][MAX_N] = {},d[MAX_N][MAX_N] = {};
int main() {
  int temp1,temp2,temp3,city ,alt,mincity ;
  long long int costtime ,cost;
  while(1){
      cin >> n;
    if(n == 0) break;
    city = mincity = 0; costtime ;
    for (int i = 0; i < n; ++i) {
      cin >> temp1 >> temp2>>temp3;
      road[temp1][temp2] = temp3;
      road[temp2][temp1] = temp3;
      if(city < temp1|| city < temp2)  {city = (temp1 > temp2)?  temp1: temp2; }
    }
    //cout << city <<endl;
    city++;

    for (int i = 0; i <city ; ++i) {
      for (int j = 0; j < city; ++j) {
        if(i != j ) d[i][j] = INFINITY;
        if (road [i][j] !=0) d[i][j] =road[i][j];
      }
    }




    for (int i = 0; i <city ; ++i) {
      for (int j = 0; j <city ; ++j) {
        for (int k = 0; k < city; ++k) {
          if(d[i][k] !=INFINITY && d[k][j] != INFINITY){
            alt = d[i][k]+ d[k][j];
            if (alt < d[i][j]) d[i][j] = alt;
          }

        }

      }
    }

    for (int i = 0; i < city; ++i) {
        cost =0;
      for (int j = 0; j < city ; ++j) {
        cost += d[i][j];
        //cout << d[i][j]<<" ";
      }
      //cout<<endl;
      if(i == 0) costtime = cost;
      if (costtime > cost) {costtime = cost;mincity = i;}
    }
    cout<< mincity << " "<<costtime<<endl;

  }


  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1000000;

using namespace std;

typedef int Weight;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src, int dst, Weight weight) :
        src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
        e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void shortestPath(const Graph &g, int s,
        vector<Weight> &dist, vector<int> &prev) {
    int n = g.size();
    dist.assign(n, INF); dist[s] = 0;
    prev.assign(n, -1);
    priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight"
    for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {
        Edge e = Q.top();
        Q.pop();
        if (prev[e.dst] != -1) continue;
        prev[e.dst] = e.src;
        FOR(f,g[e.dst]) {
            if (dist[f->dst] > e.weight+f->weight) {
                dist[f->dst] = e.weight+f->weight;
                Q.push(Edge(f->src, f->dst, e.weight+f->weight));
            }
        }
    }
}

int main(){
    int n;
    while(true){
        cin >> n;
        if(!n) break;
        Graph g(10);
        int num = 0;
        for(int i=0;i<n;++i) {
            int a,b,c;
            cin >> a >> b >> c;
            g[a].push_back(Edge(a, b, c));
            g[b].push_back(Edge(b, a, c));
            if(num < max(a,b)) num = max(a,b);
        }
        int min_index = 0;
        int min_elem = INF;
        for(int i=0;i<num;++i) {
            vector<int> dist;
            vector<int> prev;
            shortestPath(g,i,dist,prev);
            int sum = 0;
            for(int j=0;j<dist.size();++j){
                if(dist[j] < INF) sum += dist[j];
            }
            if(sum < min_elem) {
                min_elem = sum;
                min_index = i;
            }
        }
        cout << min_index << " " << min_elem << endl;
    }
}

C++: #include <bits/stdc++.h>
#define INF 114514810
#define FOR(i, r, n) for(int i=(r); i<(n); i++) 
#define REP(i, n) FOR(i, (0), n)

using namespace std;

int n, a, b, c, t = 0, sum = INF, ans, nodes = 0;
int d[50][50];

int main(){
	while (true){
		cin >> n;
		if (n == 0) break;
		REP(i, 10)
			REP(j, 10)
			d[i][j] = (i == j ? 0 : INF);
		REP(i, n){
			cin >> a >> b >> c;
			d[a][b] = d[b][a] = c;
			nodes = max(nodes, a);
			nodes = max(nodes, b);
		}
		REP(k, 10)
			REP(i, 10)
			REP(j, 10)
			d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
		REP(i, nodes + 1){
			REP(j, nodes + 1) t += d[i][j];
			if (sum > t){
				sum = t;
				ans = i;
			}
			t = 0;
		}
		cout << ans << " " << sum << endl;
		nodes = ans = 0;
		sum = INF;
	}
}

C++: #include<cstdio>
#include<cstdlib>
#include<math.h>
#include<iostream>
#include<cstring>
#include<map>
#include<set>
#include<list>
#include<stack>
#include<algorithm>
#include<queue>
#include<vector>
#include<time.h>
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
const long long INF=1e17;
typedef long long ll;
const double eps = 1e-8;
const int row=3000;
const int cow=220;
const int MAXN=cow*cow;

int n,m;

ll dp[100][100];
bool vis[100];

int main()
{
    //freopen("1","r",stdin);
    int i,j,t;
    while(scanf("%d",&m)!=EOF&&m){
        memset(vis,false,sizeof(vis));
        for(i=0;i<50;i++)for(j=0;j<50;j++)dp[i][j]=INF;
        for(i=0;i<m;i++){
            ll a,b,c;
            cin>>a>>b>>c;
            dp[a][b]=dp[b][a]=c;
            vis[a]=vis[b]=true;
        }


        for(i=0;i<50;i++){
            for(j=0;j<50;j++){
                for(t=0;t<=m;t++){
                    if(dp[i][j]!=INF&&dp[j][t]!=INF){
                        dp[i][t]=min(dp[i][t],dp[i][j]+dp[j][t]);
                    }
                }
            }
        }
        ll ans=1e18,ob=0;
        for(i=0;i<50;i++){
            ll tem=0;
            if(!vis[i])continue;
            for(j=0;j<50;j++){
                if(!vis[j])continue;
                tem+=dp[i][j];
            }
            if(tem<ans){
                ans=tem;
                ob=i;
            }
        }
        cout<<ob<<" "<<ans<<endl;
    }
    return 0;
}