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<bits/stdc++.h>
using namespace std;

enum { maxn = 20, INF = 10000000000000LL };

typedef long long LL;
LL G[maxn][maxn];
int vis[maxn];
LL d[maxn][maxn];
int inq[maxn];
int m;

void spfa(int src)
{
    memset(inq, 0, sizeof inq);
    for(int i = 0; i < maxn; i++) d[src][i] = INF;
    d[src][src] = 0LL;
    queue< int > q;
    while(!q.empty()) q.pop();
    q.push(src);
    inq[src] = 1;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        for(int i = 0; i < maxn; i++)
        {
            if(G[u][i] != -1)
            {
                if(d[src][u] + G[u][i] < d[src][i])
                {
                    d[src][i] = d[src][u] + G[u][i];
                    if(!inq[i])
                    {
                        inq[i] = 1;
                        q.push(i);
                    }
                }
            }
        }
        inq[u] = 0;
    }
}

int main()
{
    while(~scanf("%d", &m), m)
    {
        memset(vis, 0, sizeof(vis));
        memset(G, -1, sizeof(G));
        memset(d, 0, sizeof(d));
        for(int i = 0; i < m; i++)
        {
            int a, b;
            LL c;
            scanf("%d%d%lld", &a, &b, &c);
            G[a][b] = G[b][a] = c;
            vis[a] = vis[b] = 1;
        }
        for(int i = 0; i < maxn; i++)
            if(vis[i])
                spfa(i);
        LL ans = INF;
        int pt = -1;
        for(int i = 0; i < maxn; i++)
        {
            LL sum = 0;
            if(vis[i])
            {
                for(int j = 0; j < maxn; j++)
                {
                    if(vis[j])
                    {
                        sum += d[i][j];
                    }
                }
                if(sum <= ans)
                {
                    ans = sum;
                    pt = i;
                }
            }
        }
        printf("%d %lld\n", pt, ans);
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
long n,p[10][10],i,j,k,m,s,ii,ss;
while(cin>>n){
if(n==0)break;
for(i=0;i<10;i++)
for(j=0;j<10;j++)p[i][j]=p[j][i]=-1;
m=0;
for(s=k=0;k<n;k++){
cin>>i>>j;
cin>>p[i][j];
s+=p[i][j];
if(m<i)m=i;if(m<j)m=j;
}
s*=45+1;
m++;
for(i=0;i<m;i++){
p[i][i]=0;
for(j=0;j<m;j++)if(p[i][j]<0)p[i][j]=s;
}
for(k=0;k<m;k++)
for(i=0;i<m;i++)
for(j=0;j<m;j++)p[i][j]=p[j][i]=min(p[i][j],p[i][k]+p[k][j]);
for(ss=j=0;j<m;j++)ss+=p[0][j];ii=0;
for(i=0;i<m;i++){
for(s=j=0;j<m;j++)s+=p[i][j];
if(s<ss){ss=s;ii=i;}
}
cout<<ii<<" "<<ss<<endl;
}
return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>

using namespace std;
const int maxn=110;
const int INF=99999999;
int n,m;
int Map[maxn][maxn];
int Min(int x,int y)
{return x<y ? x:y;}

int main()
{
    while(cin>>m&&m!=0)
    {
        for(int i=1;i<maxn;i++)
            for(int j=1;j<maxn;j++)
            if(i==j)Map[i][j]=0;
            else Map[i][j]=INF;
        n=0;
        for(int i=0;i<m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(a+1>n)n=a+1;
            if(b+1>n)n=b+1;
            Map[a+1][b+1]=Map[b+1][a+1]=c;
        }

        for(int k=1;k<=n;k++)
            for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j&&j!=k&&k!=i)
            Map[i][j]=Min(Map[i][j],Map[i][k]+Map[k][j]);
        int mm=INT_MAX;
        int ans;
        for(int i=1;i<=n;i++)
        {
            int step=0;
            for(int j=1;j<=n;j++)
                step+=Map[i][j];
            if(step<mm)
            {
                mm=step;
                ans=i;
            }
        }
        cout<<ans-1<<' '<<mm<<endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>

using namespace std;
int inf = 0x3f3f3f3f;

int maps[50][50];
//int dis[50],vis[50];
int n;
void floyd()
{
    for(int k = 0; k<n; k++)
        for(int i = 0; i<n; i++)
            for(int j = 0; j<n; j++)
                if(maps[i][j]>maps[i][k]+maps[k][j])
                    maps[i][j]=maps[i][k]+maps[k][j];
}
int main(void)
{
    while(scanf("%d",&n)&&n)
    {
        memset(maps,inf,sizeof(maps));
        //memset(dis,inf,sizeof(dis));
        int ma = 0;
        for(int i = 0; i<n; i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            maps[x][y] = maps[y][x] = w;
            maps[i][i] = 0;
            x = max(x,y);
            ma = max(x,y);
        }
        floyd();
        int mi = inf,b=0;
        for(int i = 0;i <=ma; i++)
            {
                int sum = 0;
                for(int j = 0; j<=ma; j++)
                {
                    sum += maps[i][j];
                }
                if(sum<mi)   b = i,mi=sum;

            }

        printf("%d %d\n",b,mi);
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

const int kInf = 1 << 26;
const int kMaxV = 10;

int d[kMaxV][kMaxV];

void WarshallFloyd(){
  for(int k=0; k<kMaxV; k++){
	for(int i=0; i<kMaxV; i++){
	  for(int j=0; j<kMaxV; j++){
		d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	  }
	}
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  while(1){
	int n;
	cin >> n;
	if(n == 0){
	  return 0;
	}

	for(int i=0; i<kMaxV; i++){
	  for(int j=0; j<kMaxV; j++){
		d[i][j] = kInf;
	  }
	}

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

	int min_town = -1;
	int min_sum = kInf;
	for(int i=0; i<kMaxV; i++){
	  int sum = 0;
	  for(int j=0; j<kMaxV; j++){
		if(d[i][j] >= kInf){
		  continue;
		}
		sum += d[i][j];
	  }
	  if(sum == 0){
		break;
	  }
	  if(sum < min_sum){
		min_sum = sum;
		min_town = i;
	  }
	}
	cout << min_town << " " << min_sum << endl;
  }
  
  return 0;
}