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 = 50, INF = 1000000000 };

int G[maxn][maxn];
int vis[maxn];
int 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] = 0;
    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])
            {
                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, 0, sizeof(G));
        memset(d, 0, sizeof(d));
        for(int i = 0; i < m; i++)
        {
            int a, b, c;
            scanf("%d%d%d", &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);
        int ans = INF, pt = -1;
        for(int i = 0; i < maxn; i++)
        {
            int 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 %d\n", pt, ans);
    }
    return 0;
}

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

using namespace std;

#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define rep(i,x) for(int i=0;i<(x);++i)

const int inf = 1e9;

int d[10][10];
bool found[10];
int N;

void wf()
{
	rep(k, 10) rep(i, 10) rep(j, 10) {
		chmin(d[i][j], d[i][k] + d[j][k]);
	}
}

int main()
{
	while (scanf("%d", &N), N) {
		fill_n(found, 10, false);
		fill(d[0], d[10], inf);
		rep(i, 10) d[i][i] = 0;

		rep(i, N) {
			int a, b, c; scanf("%d %d %d", &a, &b, &c);
			d[a][b] = d[b][a] = c;
			found[a] = found[b] = true;
		}

		wf();

		int mi = inf, a;

		rep(i, 10) if (found[i]) {
			int sum = 0;
			rep(j, 10) if (found[j]) {
				sum += d[i][j];
			}
			if (mi > sum) {
				mi = sum;
				a = i;
			}
		}

		printf("%d %d\n", a, mi);
	}
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define INF 10000001
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const ull B=100000007;
struct edge{int to,cost;};
int N;
int d[10];
bool f[10];
vector<edge> G[10];
void dijkstra(int s){
      REP(i,N)d[i]=-1;
      d[s]=0;
      priority_queue<P,vector<P>,greater<P> > Q;
      Q.push(P(0,s));
      while(!Q.empty()){
            P p=Q.top();Q.pop();
            int v=p.second;
            if(d[v]>p.first)continue;
            REP(i,G[v].size()){
                  edge e=G[v][i];
                  if(d[e.to]==-1||d[e.to]>d[v]+e.cost){
                        d[e.to]=d[v]+e.cost;
                        Q.push(P(d[e.to],e.to));
                  }
            }
      }
}
int main(){
      while(cin>>N&&N){
            memset(f,false,sizeof(f));
            REP(i,N){
                  int a,b,c;
                  cin>>a>>b>>c;
                  G[a].PB((edge){b,c});
                  G[b].PB((edge){a,c});
                  f[a]=f[b]=true;
            }
            int ans=INF,pos=-1;
            REP(i,10){
                  if(!f[i])continue;
                  int sum=0;
                  dijkstra(i);
                  REP(j,10)if(f[j])sum+=d[j];
                  if(ans>sum){
                        ans=sum;
                        pos=i;
                  }
            }
            cout<<pos<<" "<<ans<<endl;
      }
}

C++: #include <cstdio>
#include <utility>
#include <queue>
#include <vector>
#include <iostream> 
#include <algorithm>
#include <math.h>
#include <map>  
#include <string.h> 

using namespace std;

int d[11][11];

const int INF=100000000;

int main() {
  
  while(1){
    
    int E; cin>>E; if(E==0) break;
    
    fill(d[0],d[11],INF);
    
    for(int i=0; i<11; i++){
      d[i][i]=0;
      }
    
    int MAX_V=0;
    
    for(int i=0; i<E; i++){
      
      int u,v,cost; cin>>u>>v>>cost;
      
      if(u>MAX_V) MAX_V=u;
      if(v>MAX_V) MAX_V=v;
      
      d[u][v]=cost; d[v][u]=cost;
      
      }
      
     for(int k=0; k<MAX_V+1; k++)
       for(int i=0; i<MAX_V+1; i++)
         for(int j=0; j<MAX_V+1; j++)  d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
         
       int A,ANS=INF;
        
      for(int i=0; i<MAX_V+1; i++){
        int ans=0;
         for(int j=0; j<MAX_V+1; j++){
           ans+=d[i][j];
           }
           if(ans<ANS) {ANS=ans; A=i;}
         }

      cout<<A<<' '<<ANS<<endl;

    }
     
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int INF=1<<25;
const int maxn=50;
int  d[maxn][maxn];
int r;
int main()
{
    while(cin>>r)
	{
		if(r==0) break;
		for(int i=0;i<=11;i++)
			for(int j=0;j<=11;j++){
				if(i!=j)
					d[i][j]=INF;
				else
					d[i][j]=0;
			}
		int n;
		n=0;
		for(int i=0;i<r;i++)
		{
			int a,b,num;
			cin>>a>>b>>num;
			int t=max(a,b);
			n=max(t,n);
			d[a][b]=num;
			d[b][a]=num;
		}
		for(int k=0;k<=n;k++)
			for(int i=0;i<=n;i++)
				for(int j=0;j<=n;j++)
					d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
		int minx=INF;
		int k;
		for(int i=0;i<=n;i++){
			int res=0;
			for(int j=0;j<=n;j++)
				res+=d[i][j];
			if(res<minx){
				minx=res;
				k=i;
			}
		}
		cout<<k<<" "<<minx<<endl;


	}
    return 0;
}