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<cstring>

const int INF=1000000000;

int d[10][10];

int dyjkstra(int start,int n);

int main()
{	
	while(1)
	{
		for(int i=0;i<10;i++)
		{
			for(int j=0;j<10;j++)
			{
				d[i][j]=INF;
			}
		}

		int n,a,b,c,townnum=0;
		bool appear[10];
		memset(appear,0,sizeof(appear));
		
		std::cin>>n;
		if(n==0)break;
		
		for(int i=0;i<n;i++)
		{
			std::cin>>a>>b>>c;
			
			d[a][b]=c;
			d[b][a]=c;
			
			if(!appear[a]){appear[a]=true;townnum++;}
			if(!appear[b]){appear[b]=true;townnum++;}
		}

		int mintown,mincost=INF;
		
		for(int i=0;i<townnum;i++)
		{
			int temp;
			temp=dyjkstra(i,townnum);
			if(temp<mincost)
			{
				mintown=i;
				mincost=temp;
			}
		}
		std::cout<<mintown<<' '<<mincost<<std::endl;
	}
	return 0;
}

int dyjkstra(int start,int n)
{
	int allcost=0;//Sum of cost[i]
	int cost[10];
	bool visited[10];

	memset(visited,0,sizeof(visited));
	for(int i=0;i<n;i++)
	{
		cost[i]=INF;
	}
	
	cost[start]=0;
	while(1)
	{
		int u=-1;
		for(int i=0;i<n;i++)
		{
			if(!visited[i]&&(u==-1||cost[u]>cost[i]))//未決定で距離最小の町u
			{
				u=i;
			}
		}
		
		if(u==-1)break;
		
		visited[u]=true;
		allcost+=cost[u];
		
		
		for(int i=0;i<n;i++)//uから行ける町のcost更新	
		{
			cost[i] = cost[i]>cost[u]+d[u][i] ? cost[u]+d[u][i] : cost[i];
		}
	}
	return allcost;
}

C++: import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main{
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			int n=sc.nextInt();//the number of roads
			Dijkstra ds=new Dijkstra(10);
			for(int i=0;i<n;i++){
				int from=sc.nextInt();
				int to=sc.nextInt();
				int cost=sc.nextInt();
				ds.addEdge(from, to, cost);
				ds.addEdge(to, from, cost);
			}
			long ans=99999999999L;
			long ans2=0;
			for(int i=0;i<n;i++){
				long a=ds.getShortestPath(i);
				if(a==0)continue;
				if(ans>a){
					ans=a;
					ans2=i;
				}
			}
			System.out.println(ans2+" "+ans);
		}
	}



	class Dijkstra{
		int n;
		long[] d;
		int s;
		PriorityQueue<Vertice> pq;
		final long INF=Long.MAX_VALUE/4;
		Dijkstra(int n){
			this.n=n;
			d=new long[n];
			@SuppressWarnings("unchecked")
			List<Edge>[]edges=new List[n];
			for(int i=0;i<n;i++){
				edges[i]=new ArrayList<Edge>();
			}
			s=-1;
		}
		public long getShortestPath(int i){
			if(i!=s){
				s=i;
				Arrays.fill(d, INF);
				d[i]=0;
				pq=new PriorityQueue<Vertice>();
				pq.add(new Vertice(i,d[i]));
				while(!pq.isEmpty()){
					Vertice u=pq.poll();
					if(d[u.me]<u.cost){
						continue;//skip old vertex
					}
					for(int ii=0;ii<edges[u.me].size();ii++){
						Edge e=edges[u.me].get(ii);
						if(d[e.from]!=INF&&e.cost+d[e.from]<d[e.to]){
							d[e.to]=e.cost+d[e.from];
							pq.add(new Vertice(e.to,d[e.to]));
						}else{
						}
					}
				}
				s=i;
			}
			int sum=0;
			for(int ii=0;ii<d.length;ii++){
				if(d[ii]==INF)continue;
				sum+=d[ii];
			}
			return sum;
		}
		void addEdge(int from,int to,long cost){
			edges[from].add(new Edge(from,to,cost));
		}
	}
	class Edge{
		int from;
		int to;
		long cost;
		Edge(int i,int j,long cost){
			this.from=i;
			this.to=j;
			this.cost=cost;
		}
	}
	class Vertice implements Comparable<Vertice>{
		int me;
		long cost;
		Vertice(int me,long cost){
			this.me=me;
			this.cost=cost;
		}
		@Override
		public int compareTo(Vertice o){
			return Long.compare(this.cost, o.cost);
		}
	}
	void tr(Object...o){System.out.println(Arrays.deepToString(o));}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INF 1e+9

int main() {
	int n;
    while(cin>>n){
        if(!n)break;

        int a,b,c,v=0,d[11][11];

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

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

        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]);
            }
        }
        int  ans1=INF,ans2;
        for(int i=0;i<=v;i++){
            int count=0;
            for(int j=0;j<=v;j++){
                count+=d[i][j];
            }
            if(ans1>count){
                ans1=count;
                ans2=i;
            }
        }

        cout<<ans2<<" "<<ans1<<endl;
    }
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3fffffff
using namespace std;

int n,m;
int ans,best;
int f[50][50];

int main()
{
    while(~scanf("%d",&n)&&n)
    {
        m=0;
        best=INF;
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++)
            {
                if(i==j)
                {
                    f[i][j]=0;
                    continue;
                }
                f[i][j]=INF;
            }
        for(int i=1;i<=n;i++)
        {
            int x,y,va;
            scanf("%d%d%d",&x,&y,&va);
            f[x][y]=f[y][x]=va;
            m=max(m,max(x,y));
        }

        for(int k=0;k<=m;k++)
            for(int i=0;i<=m;i++)
                for(int j=0;j<=m;j++)
                    f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
        for(int i=0;i<=m;i++)
        {
            int sum=0;
            for(int j=0;j<=m;j++)
                sum+=f[i][j];
            if(best>sum)
            {
                best=sum;
                ans=i;
            }
        }
        printf("%d %d\n",ans,best);
    }
}