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 <cstdio>
#include <algorithm>
#include <limits.h>
#define INF INT_MAX/100
#define FOR(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(void){
  int n,x,y,z,ans,tmp=0,nodes=0,sum=INF;
  int graph[50][50];
  char str[100];

  while(1){
    fgets(str,sizeof(str),stdin);
    sscanf(str,"%d",&n);
    if(n==0) break;
    FOR(i,10)
      FOR(j,10)
        graph[i][j] = (i==j ? 0 : 500);
    while(n--){
      fgets(str,sizeof(str),stdin);
      sscanf(str,"%d %d %d",&x,&y,&z);
      graph[x][y] = graph[y][x] = z;
      nodes = max(max(nodes,y),x);
    }
    /*REP(i,nodes+1)
      REP(j,nodes+1)
        (j!=nodes ? printf("%3d ",graph[i][j]) : printf("%3d\n",graph[i][j]));
    printf("\n");*/
    FOR(k,nodes+1){
      FOR(i,nodes+1){
        FOR(j,nodes+1){
          graph[i][j] = min(graph[i][j],graph[i][k] + graph[k][j]);
        }
      }
      //printf("\n");
    }
    FOR(i,nodes+1){
      FOR(j,nodes+1)
          tmp += graph[i][j];
      if(sum > tmp){
        sum = tmp;
        ans = i;
      }
      tmp = 0;
    }
    printf("%d %d\n",ans,sum);
    nodes = ans = 0;
    sum = INF;
  }
  return 0;
}

/* 0  1  2  3  4
  --------------
0| 0 80 60  0  0
1|80  0 20  0 90 
2|60 20  0 50  0
3| 0  0 50  0 60
4| 0 90  0 60  0  */

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;
typedef pair<int,int> P;

#define INF 1000000
struct road{int to,cost;};
vector<road> roads[10];
int d[10];
int n,m;

int main(){
  while(1){
    cin >> n;
    if(!n)break;
    m = 0;
    for(int i=0;i<n;i++){
      int x,y,z;
      scanf("%d%d%d",&x,&y,&z);
      if(m<x||m<y){
        m = max(x,y);
      }
      roads[x].push_back((road){y,z});
      roads[y].push_back((road){x,z});
    }
    m++;

    int minIndex;
    int mini = INF;
    for(int i=0;i<m;i++){
      priority_queue<P, vector<P>, greater<P> > que;
      que.push(P(0,i));
      fill(d,d+m,INF);
      d[i] = 0;
      while(que.size()){
        P p = que.top(); que.pop();
        int dist = p.first;
        int pos = p.second;
        //if(d[pos]<dist) continue;
        for(int j=0;j<roads[pos].size();j++){
          int to = roads[pos][j].to;
          int cost = roads[pos][j].cost;
          if(d[to]>d[pos]+cost){
            d[to] = d[pos] + cost;
            que.push(P(d[to],to));
          }
        }
      }
      int sum = 0;
      for(int j=0;j<m;j++){
        sum += d[j];
      }
      if(mini>sum){
        mini = sum;
        minIndex = i;
      }
    }
    cout << minIndex << " " << mini << endl;
  }
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<string.h>

using namespace std;

#define INF 1000000

int E,V,r[12][3],d[12][12];

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

void init(){
  for(int i=0;i<12;i++)
    for(int j=0;j<12;j++)
      d[i][j]=INF;

  memset(r,0,sizeof(r));
  V=0;
  E=0;
}

void solve(){
  warshall_floyd();

  int res[12];
  fill(res,res+12,0);


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


  int res1=INF;
  int res2=INF;

  for(int i=0;i<V;i++){
    if(res[i]<res2){
      res1=i;
      res2=res[i];
    }
  }

  printf("%d %d\n",res1,res2);
}

int main(){
  while(true){
    init();
    scanf("%d",&E);
    if(E==0) break;
    for(int i=0;i<E;i++){
      scanf("%d%d%d",&r[i][0],&r[i][1],&r[i][2]);
    }

    for(int i=0;i<10;i++){
      V=max(V,r[i][1]);
    }
    V++;


    for(int i=0;i<E;i++){
      d[r[i][0]][r[i][1]]=r[i][2];
      d[r[i][1]][r[i][0]]=r[i][2];
    }

    for(int i=0;i<V;i++)
      d[i][i]=0;

    solve();
  }
  return 0;
}

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

using namespace std;

const int INF = 100000000;


int d[10][10];
int ans1, ans2;
bool town[10];


main(){
   int n;
   while(cin >> n && n){
      fill(d[0], d[10], INF);
      fill(town, town+10, false);
      for(int i=0;i<n;i++){
         int a, b, c;
         cin >> a >> b >> c;
         town[a] = true;
         town[b] = true;
         d[a][b] = d[b][a] = c;
      }
      for(int k=0;k<10;k++){
         for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
               d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
         }
      }
      ans2 = INF;
      for(int i=0;i<10;i++){
         if(!town[i]) continue;
         int sum = 0;
         for(int j=0;j<10;j++){
            if(i == j) continue;
            if(!town[j]) continue;
            sum += d[i][j];
         }
         if(ans2 > sum){
            ans2 = sum;
            ans1 = i;
         }
      }
      cout << ans1 << ' ' << ans2 << endl;
   }
   return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
int maps[50][50];
int inf=0x3f3f3f3f;
void floyd(int n)
{
    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()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
        int u,v,w;
        int ma=-1;
        memset(maps,inf,sizeof(maps));
        for(int i=0;i<=50;i++) maps[i][i]=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            ma=max(ma,u);
            ma=max(ma,v);
            if(maps[u][v]>w)
            {
                maps[u][v]=maps[v][u]=w;
            }
        }
        floyd(ma);
        int mi=inf,sum,num;
        for(int i=0;i<=ma;i++)
        {
            sum=0;
            for(int j=0;j<=ma;j++)
            {
                sum=sum+maps[i][j];
            }
            if(mi>sum) mi=sum,num=i;
        }
        printf("%d %d\n",num,mi);
    }

    return 0;
}