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<queue>
using namespace std;

const int MAX_V = 100, INF = 999999;
static int cost[MAX_V][MAX_V];
static int d[MAX_V];

bool used[MAX_V];
static int V;
int E;
void dijkstra(int s) {
	fill(d, d + V, INF);
	fill(used, used + V, false);
	d[s] = 0;

	while (true) {
		int v = -1;
		for (int u = 0; u < V; u++) {
			if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
		}
		if (v == -1) break;
		used[v] = true;

		for (int u = 0; u < V; u++) {
			d[u] = min(d[u], d[v] + cost[v][u]);
		}
	}
}

int main() {
	while (1) {
		memset(cost, 999999, sizeof(cost));
		cin >> E;
		if (E == 0) break;
		int v_max = -1;
		for (int i=0 ; i < E; i++) {
			int u, v, cos;
			cin >> u >> v >> cos;
			cost[u][v] = cos;
			cost[v][u] = cos;
			if (v > v_max)v_max = v;
		}
		V = v_max + 1;
		int sum_m = 999999, m_ind = -1;
		for (int i = 0; i < V; i++) {
			dijkstra(i);
			int sum = 0;
			for (int j = 0; j < V; j++) {
				sum += d[j];
			}
			if (sum < sum_m) {
				sum_m = sum;
				m_ind = i;
			}
		}

		cout << m_ind << " " << sum_m << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
  int n;
  while(cin >>n,n){
    long long int d[10][10];
    int m=0;
    for(int i=0; i<10; i++){
      for(int j=0; j<10; j++){
	if(i == j) d[i][j] = 0;
	else d[i][j] = 1000000000;
      }
    }
    for(int i=0,a,b,c; i<n; i++){
      cin >>a>>b>>c;
      d[a][b] = c;
      d[b][a] = c;
      m = max(m,max(a,b));
    }
    for(int k=0; k<=m; k++) for(int i=0; i<=m; i++) for(int j=0; j<=m; j++) d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
    long long int ans,cost=-1;
    for(int i=0,t; i<=m; i++){
      t = 0;
      for(int j=0; j<=m; j++) t+=d[i][j];
      if(cost>t || cost == -1){cost = t;ans = i;}
    }
    cout <<ans<<" "<<cost<<endl;
  }
  return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long int ll;
typedef vector<ll> Vl;
typedef vector<int> Vi;
typedef pair<int, int> Pi;

#define INF   (1e9+7)
#define MAX_V (10)
#define MAX_E (45)
#define MAX_C (100)

int V, E;
int D[MAX_V][MAX_V];

void warshall_floyd() {
  for (size_t u = 0; u < V; u++)
    for (size_t v = 0; v < V; v++)
      for (size_t w = 0; w < V; w++) D[u][v] = min(D[u][v], D[u][w] + D[w][v]);
}

void solve() {
  int ans_c, ans_d = INF;

  warshall_floyd();

  for (size_t v = 0; v < V; v++) {
    int cost = 0;
    for (size_t u = 0; u < V; u++) cost += (D[u][v] < INF)? D[u][v]: 0;
    if (cost < ans_d) {
      ans_c = v;
      ans_d = cost;
    }
  }

  printf("%d %d\n", ans_c, ans_d);
}

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

    for (size_t u = 0; u < MAX_V; u++)
      for (size_t v = 0; v < MAX_V; v++) D[u][v] = (u == v)? 0: INF;

    V = 0;
    for (size_t i = 0; i < E; i++) {
      int v, u, cost;
      cin >> v >> u >> cost;
      D[u][v] = cost; D[v][u] = cost;
      V = max(V, (max(u, v)));
    }
    V++;

    solve();
  }

  return 0;
}

C++: #include<cstdio>
#include<iostream>

using namespace std;
#define INF 100000

int  road, max = 0, time_[10][10] = { 0 }, sum, root[10] = { 0 };

void dijkstra(int a) {

	int i,j,k=INF,used[10],minroot;
	for (i = 0; i <= max; i++) {
		used[i] = 0;
		root[i] = INF;
	}
	root[a] = 0;
	while (1) {
		minroot = INF;
		for (i = 0; i <= max; i++) {
			if (used[i] == 0&&root[i]<minroot) {
				j = i;
				minroot = root[i];
			}
			
		}
		if (k == j) {
			break;
		}
		else {
			k = j;
		}
		used[j] = 1;
		for (i = 0; i <= max; i++) {
			if (root[i] > root[j] + time_[j][i]) {
				root[i] = root[j] + time_[j][i];
			}

		}
	}
	sum = 0;
	for (i = 0; i <= max; i++)sum += root[i];

	return ;
}






int main() {
	cin >> road;
	int i, j, a, b, c, save_a[45] = { 0 }, save_b[45] = { 0 }, save_c[45] = { 0 }, min_sum = INF, ans_town;

	for (i = 1; i <= road; i++) {
		cin >> a >> b >> c;
		if (max < a) max = a;
		if (max < b) max = b;
		save_a[i] = a;
		save_b[i] = b;
		save_c[i] = c;
	}
	for (i = 0; i <= max; i++) {
		for (j = 0; j <= max; j++) {
			time_[i][j] = INF;
		}
		time_[i][i] = 0;
	}
	for (i = 1; i <= road; i++) {
		time_[save_a[i]][save_b[i]] = save_c[i];
		time_[save_b[i]][save_a[i]] = save_c[i];
	}
	for (i = 0; i <= max; i++) {
		dijkstra(i);
		if (sum < min_sum) {
			ans_town = i;
			min_sum = sum;
		}
	}
	cout << ans_town << " " << min_sum << endl;
}

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

typedef long long ll;

const int MAX_V = 10;
int d[MAX_V+10][MAX_V+10];
int n, v;

void floyd() {
    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 main() {
    while(scanf("%d", &n) != EOF && n) {
        for(int i = 0; i < MAX_V; i++) fill(d[i], d[i]+MAX_V, 100001);
        int a, b, c;
        v = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d%d%d", &a, &b, &c);
            d[a][b] = d[b][a] = c;
            v = max(v, max(a,b)+1);
        }
        for(int i = 0; i < v; i++) d[i][i] = 0;
        floyd();
        int ans = 1000000000, index = 0;
        for(int i = 0; i < v; i++) {
            int t = 0;
            for(int j = 0; j < v; j++) {
                if(i == j) continue;
                t += d[i][j];
            }
            if(ans > t) {
                ans = min(ans, t);
                index = i;
            }
        }
        printf("%d %d\n", index, ans);
    }
    return 0;
}