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

const int INF = 1000000;
const int town_max = 10;

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

		int T[town_max][town_max];
		for(int i=0;i<town_max;i++){
			for(int j=0;j<town_max;j++){
				if(i==j)
					T[i][j] = 0;
				else
					T[i][j] = INF;
			}
		}

		int m = 0;	//頂点の数
		for(int i=0;i<n;i++){
			int a, b, c;	//町の番号は0から
			cin >> a >> b >> c;
			T[a][b] = T[b][a] = c;
			if(a+1>m)
				m = a+1;
			if(b+1>m)
				m = b+1;
		}

		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				for(int k=0;k<m;k++){
					if(T[j][k]>T[j][i]+T[i][k])
						T[j][k] = T[j][i]+T[i][k];
				}
			}
		}


		int T_min = INF;    //時間の総和の最小値
		int town_min = -1;  //最小になる町の番号
		for(int i=0;i<m;i++){
			int T_sum = 0;
			for(int j=0;j<m;j++){
				T_sum += T[i][j];   //総和を計算
			}
			if(T_sum<T_min){    //最小なら
				T_min = T_sum;  //それを最小値に
				town_min = i;   //そのときの町の番号
			}
		}

		cout << town_min << " " << T_min << endl;
	}
}

C++: //can not solve problem with negative edges
//worst O(V^2)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int INF = 0x7fffffff;
const int MAXV=100+5;

int cost[MAXV][MAXV];
int d[MAXV];
bool used[MAXV];
int prev_vertex[MAXV]; //for path restore
int V;

void dijkstra(int s)
{
    fill(d, d+V, INF);
    fill(used, used+V, false);
    fill(prev_vertex, prev_vertex+V, -1);
    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)
        {
            if(cost[v][u]!=INF&&cost[v][u]!=-1&&d[u]>d[v]+cost[v][u])
            {
                d[u] = d[v]+cost[v][u];
                prev_vertex[u] = v;
            }
        }
    }
}

//get min path to t
vector<int> get_path(int t)
{
    vector<int> path;
    for(; t!=-1; t=prev_vertex[t]) path.push_back(t);
    reverse(path.begin(), path.end());
    return path;
}

int main()
{
    int n, a, b, w;
    while(scanf("%d", &n)!=EOF){
        if(n==0) break;
        memset(cost, -1, sizeof(cost));
        int minv=100,maxv=0;
        for(int i=0;i<n;++i){
            scanf("%d%d%d", &a, &b, &w);
            cost[a][b] = cost[b][a] = w;
            if(a<minv) minv = a;
            if(b<minv) minv = b;
            if(a>maxv) maxv = a;
            if(b>maxv) maxv = b;
        }
        V = maxv + 1;
        for(int i=0;i<V;++i) cost[i][i] = 0;
        int ansi = 0;
        int ansd = INF;
        for(int i=0;i<V;++i){
            dijkstra(i);
            int tmp = 0;
            for(int i=0;i<V;++i){
                tmp += d[i];
            }
            if(tmp < ansd){
                ansi = i;
                ansd = tmp;
            }
        }
        printf("%d %d\n", ansi, ansd);
    }
    return 0;
}

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

const int N = 15;
const int INF = 1 << 25;
int dist[N][N];
void chmin(int &a, int b) {
    a = std::min(a, b);
}

int main() {
    while(1) {
        int M; scanf("%d", &M);
        if(M == 0) break;

        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                if(i != j) dist[i][j] = INF;
            }
        }

        int T = 0;
        for(int i=0; i<M; i++) {
            int a, b, c; scanf("%d%d%d", &a, &b, &c);
            chmin(dist[a][b], c);
            chmin(dist[b][a], c);
            T = std::max({T, a+1, b+1});
        }

        for(int k=0; k<T; k++) {
            for(int i=0; i<T; i++) {
                for(int j=0; j<T; j++) {
                    chmin(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }

        int ans = INF, idx = 0;
        for(int i=0; i<T; i++) {
            int sum = 0;
            for(int k=0; k<T; k++) {
                sum += dist[i][k];
            }
            if(ans > sum) {
                ans = sum;
                idx = i;
            }
        }
        printf("%d %d\n", idx, ans);
    }
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#define INF 10000005
using namespace std;
int a[20][20];
int n;
int dis[20];
int Dij(int v0)
{
    //cout<<"   "<<v0<<endl;
    bool s[20];
    for(int i=0;i<=n;i++)
    {
        dis[i]=a[v0][i];s[i]=false;
    }
    dis[v0]=0;s[v0]=1;
    for(int i=1;i<=n;i++)
    {
        int mmin=INF;int u=v0;
        for(int j=0;j<=n;j++)
            if(!s[j]&&dis[j]<mmin){u=j;mmin=dis[j];}
        s[u]=1;
        for(int j=0;j<=n;j++){
            if(!s[j]&&a[u][j]<INF)
                if(dis[j]>dis[u]+a[u][j]){dis[j]=dis[u]+a[u][j];}
            //cout<<dis[j]<<endl;
        }
    }
    int sum=0;
    for(int i=0;i<=n;i++)
        sum+=dis[i];
    return sum;

}
int main()
{
    while(1)
    {
        int m;n=0;
        scanf("%d",&m);if(m==0)break;
        for(int i=0;i<20;i++)
            for(int j=0;j<20;j++)
                a[i][j]=INF;
        for(int i=0;i<m;i++)
        {
            int p,q,r;scanf("%d%d%d",&p,&q,&r);
            a[p][q]=r;a[q][p]=r;
            n=max(n,p);n=max(n,q);
        }
        int mmin=INF,p;
        //cout<<n<<endl;
        for(int i=0;i<=n;i++)
        {
            int x=Dij(i);//cout<<x<<endl;
            if(x<mmin){mmin=x;p=i;}
        }
        printf("%d %d\n",p,mmin);
    }
}

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

const int INF = 1000000000;

int main()
{
	while(true){
		int n;
		cin >> n;

		if(n==0)
			break;

		int T[10][10];
		for(int i=0; i<10; i++){
			for(int j=0; j<10; j++){
				if(i==j){
					T[i][j] = 0;
				}
				else{
					T[i][j] = INF;
				}
			}
		}

		int m = 0;		//町の総数

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

			T[a][b] = T[b][a] = c;

			if(a+1>m){
				m = a+1;
			}
			if(b+1>m){
				m = b+1;
			}
		}

        for(int l=0;l<m;l++){
            for(int i=0;i<m;i++){
                for(int j=0;j<m;j++){
                    if(T[i][j]>T[i][l]+T[l][j]){
                        T[i][j]=T[i][l]+T[l][j];
                    }
                }
            }
        }
        int sum[10];
        for(int i=0;i<m;i++){
            sum[i]=0;
            for(int j=0;j<m;j++){
                if(T[i][j]!=INF){
                    sum[i]+=T[i][j];
                }
            }
        }
        int min=sum[0];
        int k=0;
        for(int i=1;i<m;i++){
            if(min>sum[i]){
                min=sum[i];
                k=i;
            }
        }
        cout<<k<<' '<<min<<endl;




	}

	return 0;
}