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<vector>
using namespace std;
#define FOR(i,a) for(int i=0;i<(int)(a);i++)
#define pb push_back
#define clr clear()
#define sz size()
typedef unsigned int ui;

class edge{
public:
  int b,e;
  int cost;

  void set(int s,int g,int c){
    b = s;
    e = g;
    cost = c;
  }
};

class graph{
public:
  vector<int> n;
  vector<edge> e;

  void set(void){
    n.clr;
    e.clr;
  }

  int Nexist(int a){
    FOR(i,n.sz){
      if(a == n[i])return i;
    }
    return -1;
  }

  int Eexist(int a,int b){
    for(ui i=0;i<e.sz;i++){
      if(e[i].b == a && e[i].e == b)return i;
      if(e[i].b == b && e[i].e == a)return i;
    }
    return -1;
  }

  void Nadd(int a){
    if(!n.sz)n.pb(a);
    else if(Nexist(a)<0){
      ui i = 0;
      while(n[i] < a && i<n.sz)i++;
      n.insert(n.begin()+i,a);
    }
  }

  void Eadd(edge a){
    if(!e.sz)e.pb(a);
    else{
      ui i = 0;
      while(e[i].cost < a.cost && i<e.sz)i++;
      e.insert(e.begin()+i,a);
    }
    Nadd(a.b);
    Nadd(a.e);
  }

  graph prim(void){
    graph res;

    res.set();

    if(!e.sz)return res;
    else{
      res.Eadd(e[0]);

      while(res.n.sz < n.sz){
	FOR(i,e.sz){
	  if(res.Nexist(e[i].b)>=0 && res.Nexist(e[i].e)<0){
	    res.Eadd(e[i]);
	    i = 0;
	  }else if(res.Nexist(e[i].b)<0 && res.Nexist(e[i].e)>=0 ){
	    res.Eadd(e[i]);
	    i = 0;
	  }
	}
      }
      return res;
    }
  }

  vector<int> warshall_floyd(void){
    vector<int> at;
    int tmp;
    for(ui i=0;i<n.sz;i++){
      for(ui j=0;j<n.sz;j++){
	if(i==j)at.pb(0);
	else{
	  tmp = Eexist(i,j);
	  if(tmp>=0)at.pb(e[tmp].cost);
	  else at.pb(-1);
	}
      }
    }

    for(ui k=0;k<n.sz;k++){
      for(ui i=0;i<n.sz;i++){
	for(ui j=0;j<n.sz;j++){
	  if(at[i*n.sz+k] >= 0 && at[k*n.sz+j] >= 0){
	    if(at[i*n.sz+k] + at[k*n.sz+j] < at[i*n.sz+j] || at[i*n.sz+j]<0){
	      at[i*n.sz+j] = at[i*n.sz+k] + at[k*n.sz+j];
	      at[j*n.sz+i] = at[i*n.sz+k] + at[k*n.sz+j];
	    }
	  }
	}
      }
    }
    return at;
  }
};

int main(){
  int n;
  int a,b,c;
  vector<int> at;
  graph wf;
  edge tmp;
  int cost,max,minn;
  int min;

  while(1){
    cin >> n;
    if(!n)break;

    max = 0;
    wf.set();
    at.clear();
    for(int k=0;k<n;k++){
      cin >> a >> b >> c;
      if(max < a)max = a;
      if(max < b)max = b;
      tmp.set(a,b,c);
      wf.Eadd(tmp);
    }

    at = wf.warshall_floyd();

    max++;

    min = 2000000000;
    for(int i=0;i<max;i++){
      cost = 0;
      for(int j=0;j<max;j++)cost += at[i*max+j];
      if(min > cost){
	min = cost;
	minn = i;
      }
    }
    cout << minn << " " << min << endl;
  }
}

C++: #include <stdio.h>

#define TEST 0
int weakdist(int tc[10][10],int town,int from,int to)
{
    if(from ==to)
        return 0;
    if(tc[from][to] >= 0)
        return tc[from][to];
    return -1;
}

int dist(int tc[10][10],int town,int from,int to)
{
    if(from ==to)
        return 0;
    if(tc[from][to] >= 0)
        return tc[from][to];
    int mindist=-1;
    for(int via=0;via<town;via++)
    {
        if(via == from || via ==to)
            continue;
        if(tc[from][via] == -1 || tc[via][to]==-1)
            continue;
        int sumdist=dist(tc,town,from,via)
        +dist(tc,town,via,to);
        if(mindist < 0 || mindist > sumdist)
            mindist=sumdist;
    }
    tc[from][to]=mindist;
    tc[to][from]=mindist;
    return mindist;
}
void dump(int tc[10][10])
{
    
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                printf("%d ",tc[i][j]);
            }
            puts("");
        }
}

int main(void)
{
    int n;
    int tc[10][10];//connections between towns
    while(scanf("%d",&n) == 1 && n != 0)
    {
        int town=0;
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                tc[i][j]=-1;
            }
            tc[i][i]=0;
        }
        for(int k=0;k<n;k++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(a==b)
                continue;
            tc[a][b]=c;
            tc[b][a]=c;
            if(town <a+1)town=a+1;
            if(town <b+1)town=b+1;
        }
#if TEST
        printf("town.num=%d\n",town);
#endif
        int minsum=-1;
        int minnum=-1;
        for(int cnt=0;cnt <5;cnt++)
        {
            for(int i=0;i<town;i++)
            {   
                for(int j=0;j<town;j++)
                {
                    dist(tc,town,i,j);
                }
            }
        }//try calculating for setting the map.
        for(int i=0;i<town;i++)
        {
            int sum=0;
            for(int j=0;j<town;j++)
            {
                sum+=dist(tc,town,i,j);
            }
            if(minsum<0 || minsum >sum)
            {
                minsum=sum;
                minnum=i;
            }
        }
#if TEST
        dump(tc);
#endif
        printf("%d %d\n",minnum,minsum);
    }
    return 0;

}

C++: #include <stack>
#include <utility>
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <vector>
#include <queue>
#include <functional>
#include <cstdio>
using namespace std;

int main()
{
	int n;
	typedef pair <int, int> p;//????????????????????????
	while (cin >> n, n != 0) {
		vector <p> map[12];
		int a, b, c;
		for (int i = 0; i < n; i++) {
			cin >> a >> b >> c;
			map[a].push_back(p(b, c));
			map[b].push_back(p(a, c));
		}
		p ans = p(0, 9999999);
		for (int z = 0; z < 10; z++) {
			if (map[z].size() == 0)break;
			int d[12] ;
			for (int i = 0; i < 12; i++)d[i] = 9999999;
			priority_queue<p, vector<p>, greater<p>> que;//??????????????????
			que.push(p(0, z));
			d[z] = 0;
			while (!que.empty()) {
				p now = que.top();
				que.pop();
				if (now.first > d[now.second])continue;
				for (int i = 0; i < map[now.second].size(); i++) {
					p dst = map[now.second][i];
					if (d[dst.first] > now.first + dst.second) {
						que.push(p(now.first + dst.second, dst.first));
						d[dst.first] = now.first + dst.second;
					}
				}
			}int cnt = 0;
			for (int i = 0; i < 10; i++) { if (d[i] != 9999999)cnt += d[i]; }
			if (ans.second > cnt)ans = p(z, cnt);
		}
		cout << ans.first << " " << ans.second << endl;
	}
}