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++: #define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,string> P;

static const double eps = 1e-8;

int path[10][10];
int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		memset(path,0x3,sizeof(path));
		int m=-1;
		for(int i=0;i<n;i++){
			int from,to,cost;
			scanf("%d %d %d",&from,&to,&cost);
			path[from][to] = path[to][from] = min(path[from][to],cost);
			m = max(from,max(to,m));
		}

		for(int k=0;k<=m;k++){
			for(int i=0;i<=m;i++){
				for(int j=0;j<=m;j++){
					if(i==j) path[i][j] = 0;
					else path[i][j] = min(path[i][k]+path[k][j],path[i][j]);
				}
			}
		}

		int mincost = INF;
		int idx=0;
		for(int i=0;i<=m;i++){
			int sum=0;
			for(int j=0;j<=m;j++){
				sum+=path[i][j];
			}
			if(sum < mincost){
				mincost = sum;
				idx = i;
			}
		}

		printf("%d %d\n",idx,mincost);
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF (1<<21)
int main(){
	int m;
	while(cin >> m , m){
		int wf[10][10] = {0};
		rep(i,10)rep(j,10)wf[i][j] = INF;
		rep(i,10)wf[i][i] = 0;
		int n = 0;
		rep(i,m){
			int a,b,c;
			cin >> a >> b >> c;
			wf[a][b] = wf[b][a] = c;
			n = max(n,max(a,b)+1);
		}
		rep(k,n)rep(i,n)rep(j,n)
			wf[i][j] = min(wf[i][j] , wf[i][k]+wf[k][j]);
		
		pair<int,int> d = make_pair(INF,-1);

		rep(i,n){
			int sum = 0;
			rep(j,n)sum += wf[i][j];
			d = min(d,make_pair(sum,i));
		}
		cout << d.second << " " << d.first << endl;
	}
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1000000000;
const double PI = acos(-1);
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

const int MAX_V = 10;
LL d[MAX_V][MAX_V];//d[i][j]???e={u,v}????????????(?????¨???????????¨??????INF,i==j?????¨??????0)
int v;



int main() {
	while (cin >> v&&v) {
		REP(i, MAX_V)REP(j, MAX_V) {
			if (i == j)d[i][j] = 0;
			else d[i][j] = INF;
		}
		int a, b, c;
		REP(i,v) {
			cin >> a >> b >> c;
			d[a][b] = c;
			d[b][a] = c;
		}
		/*REP(i, MAX_V) {
			REP(j, MAX_V) {
				cout << d[i][j] << " ";
			}
			cout << endl;
		}*/
		for (int k = 0; k<10; ++k) {
			for (int i = 0; i<10; ++i) {
				for (int j = 0; j<10; ++j) {
					d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
				}
			}
		}
		LL ans = INF;
		int ans2;
		REP(i, MAX_V) {
			LL num = 0;
			REP(j, MAX_V) {
				if (d[i][j] == INF)continue;
				num += d[i][j];
			}
			if (num != 0 && ans > num) {
				ans = num;
				ans2 = i;
			}
		}
		cout << ans2 << " " << ans << endl;
	}
}

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

class Node{
public:
	vector< int > e_to;
	vector< int > e_cost;
	
	bool def;
	int min_cost;
	
	Node(){
		def = false;
		min_cost = -1;
	}
};
bool operator==(const Node& ot1, const Node& ot2){
	return ot1.min_cost == ot2.min_cost;
}
bool operator!=(const Node& ot1, const Node& ot2){
	return !(ot1 == ot2);
}
bool operator<(const Node& ot1, const Node& ot2){
	return ot1.min_cost < ot2.min_cost;
}
bool operator>(const Node& ot1, const Node& ot2){
	return ot1.min_cost > ot2.min_cost;
}
bool operator<=(const Node& ot1, const Node& ot2){
	return !(ot1 > ot2);
}
bool operator>=(const Node& ot1, const Node& ot2){
	return !(ot1 < ot2);
}

class Dijkstra{
	priority_queue< Node, vector< Node >, greater< Node > > que;
	vector< Node > nodes;
	
public:
	void Init(int node_num){
		nodes.resize(node_num);
		for(int i = 0; i < node_num; ++i){
			nodes[i] = Node();
		}
	}
	
	void Put(int id, int to, int cost){
		nodes[id].e_to.push_back(to);
		nodes[id].e_cost.push_back(cost);
	}
	
	int Run(int s){
		int sum = 0;
		
		for(vector< Node >::iterator it = nodes.begin(); it != nodes.end(); ++it){
			it->min_cost = -1;
			it->def = false;
		}
		
		nodes[s].min_cost = 0;
		que.push(nodes[s]);
		
		while(!que.empty()){
			Node def_node = que.top();
			que.pop();
		
			if(def_node.def)continue;
		
			def_node.def = true;
		
			for(int i = 0; i < def_node.e_to.size(); ++i){
				int to = def_node.e_to[i];
				int cost = def_node.min_cost + def_node.e_cost[i];
				
				if(nodes[to].min_cost == -1 || cost < nodes[to].min_cost){
					nodes[to].min_cost = cost;
					que.push(nodes[to]);
				}
			}
		}
		
		for(vector< Node >::iterator it = nodes.begin(); it != nodes.end(); ++it){
			if(it -> min_cost != -1){
				sum += it->min_cost;
			}
		}
		
		return sum;
	}
};

int main(){
	int n, s, t, w;
	Dijkstra dijk;
	
	while(cin >> n, n){
		dijk.Init(10);
		int m = 0;
		int min = ~(1 << 31);
		int mint;
		
		for(int i = 0; i < n; ++i){
			cin >> s >> t >> w;
			dijk.Put(s, t, w);
			dijk.Put(t, s, w);
			m = max(max(m, s), t);
		}
		
		for(int i = 0; i < m + 1; ++i){
			int r = dijk.Run(i);
			
			if(r < min){
				min = r;
				mint = i;
			}
		}
		cout << mint << " " << min << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define inf 1000000000
using namespace std;
unsigned long long d[10][10];
int v;

int main()
{
	int n;
	while(cin>>n,n)
	{
		bool used[10];
		memset(used,0,sizeof(used));
		memset(d,0,sizeof(d));
		for(int i=0;i<n;i++)
		{
			int s,t,c;
			cin>>s>>t>>c;
			used[s]=used[t]=true;
			d[s][t]=d[t][s]=c;
		}
		v=0;
		for(int i=0;i<10;i++)
		{
			if(used[i]) v++;
			for(int j=0;j<10;j++)
			{
				if(d[i][j]==0&&i!=j) d[i][j]=inf;
			}
		}

		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 ans;
		unsigned long long mn=inf;
		for(int i=0;i<v;i++)
		{
			unsigned long long sum=0;
			for(int j=0;j<v;j++)
			{
				sum+=d[i][j];
			}
			if(sum<mn)
			{
				ans=i;mn=sum;
			}
		}
		cout<<ans<<" "<<mn<<endl;

	}
	return 0;
}