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 <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
int INF = 1 << 10;

vector<int> d(10, INF);
struct edge{
  int to;
  int cost;
};

vector<vector<edge> > G(10);
vector<P> ans;

void init(){
  for(int i = 0; i < 10; i++){
	d[i] = INF;
  }
}

void display_d(){
  for(int i = 0; i < 10; i++){
	if(d[i] == INF) cout << "INF ";
	else cout << d[i] << " ";
  }
  cout << endl;
}

void initG(){
  for(int i = 0; i < 10; i++){
	G[i].clear();
  }
}

void dijkstra(int s){
  priority_queue<P, vector<P>, greater<P> > que;
  init();
  d[s] = 0;
  que.push(P(0, s));
  while(!que.empty()){
	P p = que.top(); que.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;
		que.push(P(d[e.to], e.to));
	  }
	}
  }
}

int main(){
  int n = 0;
  while(1){
	cin >> n;
	if(n == 0) break;
	int a = 0, b = 0, c = 0;
	for(int i = 0; i < n; i++){
	  cin >> a >> b >> c;
	  edge e1, e2;
	  e1.to = b; e1.cost = c;
	  e2.to = a; e2.cost = c;
	  G[a].push_back(e1);
	  G[b].push_back(e2);
	}

	for(int i = 0; i < 10; i++){
	  dijkstra(i);
	  int sum = 0;
	  for(int i = 0; i < 10; i++){
		if(d[i] == INF) continue;
		sum += d[i];
	  }
	  if(sum != 0) ans.push_back(P(sum, i));
	}

	sort(ans.begin(), ans.end());
	cout << ans[0].second << " " << ans[0].first << endl;
	initG();
  }
  return 0;
}

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

typedef pair<int,int> P;

#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)

struct edge {
  int from, to, cost;
};

const int INF = 10000000;
int n, d[10];

void solve() {
  vector<edge> es(2*n);
  
  int towns = 0;
  rep(i,n) {
    int f, t, c; cin>>f>>t>>c;
    es[i*2].from = f; es[i*2].to = t; es[i*2].cost = c;
    es[i*2+1].from = t; es[i*2+1].to = f; es[i*2+1].cost = c;
    towns = max(towns, max(f,t));
  }
    
  int town, mini = INF;
  rep(z,towns+1) {
    rep(i,10) d[i] = INF;
    priority_queue<P> que;
    que.push(P(0, z));
    while (!que.empty()) {
      P p = que.top(); que.pop();
      int cc = p.first, ii = p.second;
      if (cc > d[ii]) continue;
      d[ii] = cc;
      rep(i,n*2) {
        edge e = es[i];
        if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
          d[e.to] = d[e.from] + e.cost;
          que.push(P(d[e.to], e.to));
        }
      }
    }
    
    int sum = 0;
    rep(i,towns+1) sum += d[i];
    if (sum < mini) {
      town = z;
      mini = sum;
    }
  }
  cout<<town<<" "<<mini<<endl;
}

int main (int argc, char const* argv[]) {
  while (cin>>n, n) {
    solve();
  }
  return 0;
}

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

#define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++)
#define ALL(x) x.begin(),x.end()
template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; }
template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; }
typedef long long ll;

int N,M;
int edge [10] [10];
const int INF = 1e9;

int main()
{
	while(scanf("%d",&M),M){
		fill(edge [0],edge [10],INF);
		N = 0;
		FOR(i,0,M){
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			chmin(edge [u] [v],w);
			chmin(edge [v] [u],w);
			chmax(N,max(u,v) + 1);
		}
		FOR(i,0,N) edge [i] [i] = 0;

		FOR(k,0,N) FOR(i,0,N) FOR(j,0,N){
			chmin(edge [i] [j],edge [i] [k] + edge [k] [j]);
			chmin(edge [j] [i],edge [i] [k] + edge [k] [j]);
		}

		int mn = INF,ans = -1;
		FOR(i,0,N){
			int sum = 0;
			FOR(j,0,N) if(edge [i] [j] < INF){
				sum += edge [i] [j];
			}
			if(sum < mn){
				mn = sum;
				ans = i;
			}
		}

		printf("%d %d\n",ans,mn);
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int main(){
	while(true){
		int n;
		cin >> n;

		if(n == 0){
			break;
		}

		int* start = new int[n];
		int* goal = new int[n];
		int* cost = new int[n];

		for(int i = 0; i < n; i++){
			cin >> start[i] >> goal[i] >> cost[i];
		}

		int town;

		for(int i = 0; i < n; i++){
			town = max(start[i], goal[i]);
		}

		town++;

		vector<vector<int> > dist(town, vector<int>(town, INT_MAX / 2));

		for(int i = 0; i < n; i++){
			dist[start[i]][goal[i]] = cost[i];
			dist[goal[i]][start[i]] = cost[i];
		}

		for(int i = 0; i < town; i++){
			dist[i][i] = 0;
		}


		for(int k = 0; k < town; k++){
			for(int i = 0; i < town; i++){
				for(int j = 0; j < town; j++){
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
				}
			}
		}
		int* total = new int[town];
		int TOTAL = INT_MAX / 2;
		int S;

		for(int i = 0; i < town; i++){
			total[i] = 0;
		}

		for(int i = 0; i < town; i++){
			for(int j = 0; j < town; j++){
				total[i] += dist[i][j];
			}
			if(TOTAL > total[i]){
				TOTAL = min(TOTAL, total[i]);
				S = i;
			}
		}

		cout << S << " " << TOTAL << endl;
		delete [] start;
		delete [] goal;
		delete [] cost;
	}
}