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)
	}
}

Python: while True:
    N = int(input())
    if N == 0:
        break

    INF = 1000000000

    mat = []

    for i in range(10):
        row = []
        for j in range(10):
            if i == j:
                row.append(0)
            else:
                row.append(INF)
        mat.append(row)

    for i in range(N):
        inputs = input().split()
        s = int(inputs[0])
        e = int(inputs[1])
        r = int(inputs[2])
        mat[s][e] = r
        mat[e][s] = r

    for k in range(N):
        for i in range(N):
            for j in range(N):
                mat[i][j] = min(mat[i][j], mat[i][k] + mat[j][k])

    ans_num = 0
    ans = INF * 10

    for i in range(10):
        tmp_ans = 0
        for j in mat[i]:
            tmp_ans += j
        if ans > tmp_ans:
            ans_num = i
            ans = tmp_ans

    ans = 0
    for i in mat[ans_num]:
        if i != INF:
            ans += i

    print("%s %s" % (ans_num, ans))

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0189

"""
import sys
from sys import stdin
input = stdin.readline


def warshallFloyd(V, dp):
    for k in range(V):
        for i in range(V):
            for j in range(V):
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])


def main(args):
    while True:
        n_max = 9+1
        n = int(input())
        if n == 0:
            break
        dp = [[float('inf')] * n_max for _ in range(n_max)]
        for i in range(n_max):
            dp[i][i] = 0

        max_town = 0
        for _ in range(n):
            a, b, c = map(int, input().split())
            dp[a][b] = c
            dp[b][a] = c
            max_town = max(max_town, a, b)

        warshallFloyd(max_town + 1, dp)


        min_dist = float('inf')
        town_live = -1
        for i, d in enumerate(dp[:max_town+1]):
            sum_dist = 0
            for ele in d[:max_town+1]:
                sum_dist += ele
            if sum_dist < min_dist:
                min_dist = sum_dist
                town_live = i

        print('{} {}'.format(town_live, min_dist))



if __name__ == '__main__':
    main(sys.argv[1:])

Python: NULL = 10000
TOWN_COUNT = 10

while True:

    input_count = int(input())

    if input_count == 0:
        break

    adjacency_matrix = [[NULL] * TOWN_COUNT for _ in range(TOWN_COUNT)]

    for lp in range(TOWN_COUNT):
        adjacency_matrix[lp][lp] = 0

    need_range = 0

    for _ in range(input_count):
        start, end, time = [int(item) for item in input().split(" ")]

        adjacency_matrix[start][end] = time
        adjacency_matrix[end][start] = time

        need_range = max(need_range, start, end)

    for a in range(need_range + 1):
        for b in range(need_range + 1):
            for c in range(need_range + 1):
                adjacency_matrix[b][c] = min(adjacency_matrix[b][a] + adjacency_matrix[a][c], adjacency_matrix[b][c])

    min_time = NULL
    min_index = -1

    for index, item in enumerate(adjacency_matrix):
        need_element = [item2 for item2 in item if 0 < item2 < NULL]

        if 0 < len(need_element):
            time = sum(need_element)

            if time < min_time:
                min_time = time
                min_index = index

    print(min_index, min_time)

Python: class WarshallFloyd():
    """Warshall-Floyd Algorithm: 
       find the lengths of the shortest paths between all pairs of vertices
       used in GRL1C(AOJ), aoj0189
    """

    def __init__(self, V, E, INF=10**9):
        """ V: the number of vertexes
            E: adjacency list
            start: start vertex
            INF: Infinity distance
        """
        self.V = V
        self.E = E
        self.warshall_floyd(INF)

    def warshall_floyd(self, INF):
        self.distance = [[INF] * self.V for _ in range(self.V)]
        for i in range(self.V): self.distance[i][i] = 0
        for fr in range(self.V):
            for to, cost in self.E[fr]:
                self.distance[fr][to] = cost
        for k in range(self.V):
            for i in range(self.V):
                for j in range(self.V):
                    if self.distance[i][k] != INF and self.distance[k][j] != INF:
                        self.distance[i][j] = min(self.distance[i][j], self.distance[i][k] + self.distance[k][j])

    def hasNegativeCycle(self):
        for i in range(self.V):
            if self.distance[i][i] < 0:
                return True
        else:
            return False
                
while True:
    E = int(input())
    if E == 0: break
    V = 0
    edge = [[] for _ in range(10)]
    for _ in range(E):
        s, t, cost = map(int, input().split())
        V = max(s, t, V)
        edge[s].append((t, cost))
        edge[t].append((s, cost))
    allsp = WarshallFloyd(V+1, edge)
    ans = (10**9, 10**9)
    for i in range(V+1):
        ans = min(ans, (sum(allsp.distance[i]), i))
    print(*ans[::-1])

Python: INF = float('inf')
TOWN_NUM = 10

while True:
    way_num = int(input())
    if way_num == 0:
        break

    table = [[INF for i in range(TOWN_NUM)] for j in range(TOWN_NUM)]

    for _ in range(way_num):
        a,b,c = [int(i) for i in input().split()]
        table[a][b] = c
        table[b][a] = c

    for k in range(TOWN_NUM):
        for i in range(TOWN_NUM):
            for j in range(TOWN_NUM):
                if table[i][j] > table[i][k] + table[k][j]:
                    table[i][j] = table[i][k] + table[k][j]

    answer = INF
    answer_id = 0
    for i in range(TOWN_NUM):
        tmp = 0
        flag = False
        for j in range(TOWN_NUM):
            if table[i][j] < INF and i != j:
                tmp = tmp + table[i][j]
                flag = True
        if tmp < answer and flag:
            answer = tmp
            answer_id = i

    print(answer_id, answer)

Python: INF = 10 ** 10
while True:
  n = int(input())
  if n == 0:
    break

  costs = [[INF] * 10 for _ in range(10)]
  for i in range(10):
    costs[i][i] = 0
  
  city = set()
  for _ in range(n):
    a, b, c = map(int, input().split())
    city = city | {a, b}
    costs[a][b] = c
    costs[b][a] = c 
  
  city = sorted(list(city))
  
  for k in range(10):
    for i in range(10):
      for j in range(10):
        costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j])
  
  min_total = INF
  best_city = -1
  for fr in city:
    total = 0
    for to in city:
      total += costs[fr][to]
    if total < min_total:
      min_total = total
      best_city = fr
  print(best_city, min_total)