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 <climits>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	int n;
	while( true ) {
		cin >> n;
		if(n == 0) {
			break;
		}

		vector<int> a(n), b(n), c(n);

		for(int i = 0; i < n; i++) {
			cin >> a[i] >> b[i] >> c[i];
		}

		vector< vector< int > > map(10, vector< int >(10, INT_MAX / 4));

		for(int i = 0; i < n; i++) {
			map[a[i]][b[i]] = c[i];
			map[b[i]][a[i]] = c[i];
		}
		for(int i = 0; i < 10; i++) {
			map[i][i] = 0;
		}

		for(int k = 0; k < 10; k++) {
			for(int i = 0; i < 10; i++) {
				for(int j = 0; j < 10; j++) {
					map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
				}
			}
		}

		int total[10];

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

		int min_cost = INT_MAX / 2, town;

		for(int i = 0; i < 10; i++) {
			for(int j = 0; j < 10; j++) {
				if(map[i][j] >= INT_MAX / 4) {
					continue;
				}
				total[i] += map[i][j];
			}
		}

		for(int i = 0; i < 10; i++) {
			if(total[i] == 0) {
				continue;
			}
			if(min_cost > total[i]) {
				min_cost = total[i];
				town = i;
			}
		}

		cout << town << " " << total[town] << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
const int INF=1e8;
int d[12][12];
int V;
void warshall_floyd()
{
	for(int k=0;k<12;k++)
	for(int i=0;i<12;i++)
	for(int j=0;j<12;j++)
	d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
int main()
{
	while(cin>>V)
	{
		if(V==0) break;
		for(int i=0;i<12;i++)
		for(int j=0;j<12;j++)
		{
			if(i==j) d[i][j]=0;
			else d[i][j]=INF;
		}
		for(int i=0;i<V;i++)
		{
			int a,b,c;
			cin>>a>>b>>c;
			d[a][b]=c; d[b][a]=c;
		}
		warshall_floyd();
		int k,s[12]={0},min=INF;
		for(int i=0;i<12;i++)
		{
			for(int j=0;j<12;j++)
			if(d[i][j]!=INF) s[i]+=d[i][j];
			if(s[i]!=0&&s[i]<min) {min=s[i];k=i;}
		}
		cout<<k<<' '<<min<<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)printf("0\n");
        else printf("%d\n",ans);

    }


};

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

#define INF 1000000

using namespace std;

int main(void){
	int n;
	while(cin >> n && n){
		int f[10][10];
		fill((int*)f, (int*)f+n*n, INF);
		int s,t,cost;
		int maxi=0;
		for(int i=0;i<n;i++){
			cin >> s >> t >> cost;
			maxi = max(max(maxi,s),t);
			f[s][t] = cost;
			f[t][s] = cost;
		}
		for(int i=0;i<=maxi;i++) f[i][i] = 0;
		//WF
		for(int k=0;k<=maxi;k++){
			for(int i=0;i<=maxi;i++){
				for(int j=0;j<=maxi;j++){
					f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
				}
			}
		}
		int tmp,index;
		int ret = INF;
		for(int i=0;i<=maxi;i++){
			tmp = 0;
			for(int j=0;j<=maxi;j++){
				tmp += f[i][j];
			}
			if(tmp < ret){
				ret = tmp;
				index = i;
			}
		}
		cout << index << " " << ret << endl;
	}
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define SF scanf
#define PF printf
#define MAXN 50
#define INF 0x3FFFFFFF
using namespace std;
int f[MAXN][MAXN];
int n,m;
int main(){
    SF("%d",&n);
    while(n){
        int x,y,v;
        for(int i=0;i<=9;i++)
            for(int j=0;j<=9;j++)
                f[i][j]=INF;
        for(int i=1;i<=n;i++){
            SF("%d%d%d",&x,&y,&v);
            f[x][y]=min(f[x][y],v);
            f[y][x]=min(f[y][x],v);
        }
        for(int i=0;i<=9;i++)
            for(int j=0;j<=9;j++)
                for(int k=0;k<=9;k++)
                    if(i!=j&&k!=j&&k!=i)
                        f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
        int ans=-1,bestid;
        for(int i=0;i<=9;i++){
            int ans1=-1;
            for(int j=0;j<=9;j++){
                if(f[i][j]==INF||i==j)
                    continue;
                if(ans1==-1)
                    ans1=0;
                ans1+=f[i][j];
                //PF("(%d %d %d)\n",i,j,f[i][j]);
            }
            if(ans1!=-1&&(ans1<ans||ans==-1)){
                bestid=i;
                ans=ans1;
            }
        }
        PF("%d %d\n",bestid,ans);
        SF("%d",&n);
    }
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXC 12
int N,dis[MAXC][MAXC];
bool city[MAXC];
int main()
{
	while(~scanf("%d",&N)&&N)
	{
		memset(dis,0x3F,sizeof dis);
		memset(city,0,sizeof city);
		for(int i=1;i<MAXC;i++)
			dis[i][i]=0;
		for(int i=1,a,b,c;i<=N;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			city[a]=1;city[b]=1;
			dis[a][b]=min(dis[a][b],c);
			dis[b][a]=min(dis[b][a],c);
		}
		for(int k=0;k<MAXC;k++)
		{
			if(!city[k])continue;
			for(int i=0;i<MAXC;i++)
			{
				if(!city[i])continue;
				for(int j=0;j<MAXC;j++)
				{
					if(!city[j]&&dis[i][k]<0x3F3F3F3F&&dis[k][j]<0x3F3F3F3F)continue;
					dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
				}
			}
		}
		int mn=0x7FFFFFFF,id,sum;
		for(int i=0;i<MAXC;i++)
			if(city[i])
			{
				sum=0;
				for(int j=0;j<MAXC;j++)
					if(city[j])
						sum+=dis[i][j];
				if(mn>sum)
				{mn=sum;id=i;}
			}
		printf("%d %d\n",id,mn);
	}
	return 0;
}