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>
#include <vector>
#include <queue>
#define MAX_V 11
#define INF (1e9)
#define cost first
#define node second
using namespace std;
typedef pair <int, int> P;
int n,m;
vector<P> G[101];

//prim
int prim(){
  int res = 0;
  bool visited[101]={};
  int D[101];
  for(int i=0;i<n;i++) D[i] = INF;
  priority_queue <P, vector<P>, greater<P> > Q;
  Q.push(P(0,0));
  while(!Q.empty()){
    P t = Q.top(); Q.pop();
    if(visited[t.node]) continue;
    visited[t.node] = true;
    res += t.cost;
    for(int i=0;i<G[t.node].size();i++){
      int nnode = G[t.node][i].node;
      int ncost = G[t.node][i].cost;
      if(D[nnode] > ncost) Q.push(P(ncost,nnode)),D[nnode]=ncost;
    }
  }
  return res;
}

/*
void init(int n){
  for(int i=0;i<n;i++) G[i].clear();
}
*/

// warshall froyd
	  
int d[MAX_V][MAX_V];
int used[MAX_V];

void init(){
  for(int i=0;i<MAX_V;i++)
    for(int j=0;j<MAX_V;j++) i!=j ? d[i][j]=INF : d[i][j]=0;
}
  
void warshall_froyd(){
  for(int k=0;k<MAX_V;k++)
    for(int i=0;i<MAX_V;i++)
      for(int j=0;j<MAX_V;j++) d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}



int main() {
  while(1){
    cin >> n;
    if(!n)break;
    init();
    int v = 0;
    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(a,max(b,v));
    }
    warshall_froyd();
    int ans = INF;
    int node=-1;
    for(int i=0;i<=v;i++){
      int sum = 0;
      for(int j=0;j<=v;j++) sum+= d[i][j];
      if(ans > sum) ans=sum , node = i;
    }
    cout <<node <<" "<< ans <<endl;
  }
  return 0;
}

C++: //C++14 (Clang 3.8.0)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <climits>
using namespace std;
typedef pair<int,int> p;
int inf=INT_MAX;
int main(){
    while(1){
        int n;
        scanf("%d",&n);
        if(n==0) break;

        int d[50][50];

        for(int i=0;i<50;i++){
            for(int j=0;j<50;j++)d[i][j]=inf;
        }
        for(int i=0;i<50;i++)d[i][i]=0;
        for(int i=0;i<n;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            d[a][b]=c;
            d[b][a]=c;
        }
        for(int k=0;k<50;k++){
            for(int i=0;i<50;i++){
                for(int j=0;j<50;j++){
                    if(d[i][k]!=inf and d[k][j]!=inf) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
                }
            }
        }
        int ans=inf;
        for(int i=0;i<50;i++){
            int tmp=0;
            int flag=0;
            for(int j=0;j<50;j++){
                if(d[i][j]!=inf and i!=j){tmp+=d[i][j];flag=1;}
            }
            if(flag==1)ans=min(ans,tmp);
        }
        if(ans==inf)print("0\n");
        else printf("%d\n",ans);

    }


};

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<limits.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<set>
#include<vector>
#include<queue>
#include<set>
#include<stdlib.h>
#include<time.h>
using namespace std;
typedef long long ll;
#define inf	1000000
typedef pair<int,int>	P;
int road[10][10];
int n;
int used[10];
void init(){
	memset(used,false,sizeof(used));
	fill_n(&road[0][0],10*10,1000);
	for(int i=0;i<10;i++){
		road[i][i]=0;
	}
}

void floyd(){
	for(int k=0;k<10;k++){
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				if(used[k]&&used[i]&&used[j]){
					if(road[i][k]+road[k][j]<road[i][j]){
						road[j][i]=road[i][j]=road[i][k]+road[k][j];
					}
				}
			}
		}
	}
}

void solve(int &city,int &cost){
	floyd();
	for(int i=0;i<10;i++){
		if(used[i]){
			int sum=0;
			for(int j=0;j<10;j++){
				if(used[j]){
					sum+=road[i][j];
				}
			}
			if(sum<cost){
				cost=sum;
				city=i;
			}else if(sum==cost){
				city=min(city,i);
			}
		}
	}
}

int main() {
	
	int a,b,c;
	while(scanf("%d",&n)!=EOF){
		if(n==0){
			break;
		}
		init();
		while(n--){
			scanf("%d%d%d",&a,&b,&c);
			road[a][b]=road[b][a]=c;
			used[a]=used[b]=true;
		}
		int city=10,cost=INT_MAX;
		solve(city,cost);
		printf("%d %d\n",city,cost);
	}
	return 0;
}

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

using namespace std;


int main(){

    int n;
    vector< vector<int> > map(100,vector<int>(100));
    int a,b,c;
    vector<int> v(100);
    fill(v.begin(),v.end(),1<<20);
    while(true){
	fill(map.begin(),map.end(),v);
	cin >> n;
	int ma=0;
	if(n == 0) break;
	for(int i=0;i<n;i++){
	    cin >> a >> b >> c;
	    map[a][b] = c;
	    map[b][a] = c;
	    ma = max(ma,b);
	}
	for(int i=0;i<ma;i++){
	    map[i][i] = 0;
	}
	ma++;
	for(int i=0;i<ma;i++){
	    for(int j=0;j<ma;j++){
		for(int k=0;k<ma;k++){
			map[i][j] = min(map[i][j] ,map[i][k] + map[k][j] );
		}
	    }
	}

	int res = 1<<30,t=0;
	for(int i=0;i<ma;i++){
	    a =0;
	    for(int j=0;j<ma;j++){
		a += map[i][j];
	    }
	    if(a!=0 && res>a){
		res = a;
		t = i;
	    }
	}
	cout << t<<" " << res << endl;
    }
    
    return 0;
}