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: 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_index = 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_index, 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[a][c] = min(adjacency_matrix[a][b] + adjacency_matrix[b][c], adjacency_matrix[a][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: while True:
    N = int(input())
    if N == 0:
        break

    abc = []
    cities = [False] * 10
    for i in range(N):
        a, b, c = list(map(int, input().split()))
        cities[a] = True
        cities[b] = True
        abc.append([a, b, c])

    cnum = cities.count(True)

    # G[i][j]: iとjの最短経路
    G = []
    for i in range(cnum):
        a = [float('inf')] * cnum
        G.append(a)

    for i in range(cnum):
        G[i][i] = 0

    for a, b, c in abc:
        G[a][b] = min(G[a][b], c)
        G[b][a] = min(G[b][a], c)


    for k in range(cnum):
        for i in range(cnum):
            for j in range(cnum):
                G[i][j] = min(G[i][j], G[i][k] + G[k][j])

    # for i in range(cnum):
    #     print(G[i])
    min_city = None
    min_cost = float('INF')
    for i in range(cnum):
        c = sum(G[i])
        if c < min_cost:
            min_cost = c
            min_city = i

    print(min_city, min_cost)

Python: road = int(input())
while road > 0:
    answer = [[float("inf") if n != m else 0 for n in range(10)] for m in range(10)]
    city = 0
    for _ in range(road):
        c1, c2, w = (int(n) for n in input().split(" "))
        answer[c1][c2] = w
        answer[c2][c1] = w
        city = max(city, c1, c2)
    for i in range(city):
        for j in range(city):
            for k in range(city):
                answer[j][k] = min(answer[j][k], answer[j][i] + answer[i][k])
    sum_ans = [-1 , float("inf")]
    for i in range(city):
        if sum(answer[i][:city]) < sum_ans[1]:
            sum_ans = [i, sum(answer[i][:city])]
    print(*sum_ans)
    road = int(input())

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0189
AC
"""
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_ik = dp[i][k]
                dp_kj = dp[k][j]
                if dp[i][j] > dp_ik + dp_kj:
                    dp[i][j] = dp_ik + dp_kj


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: def solve(_dp, _v):
    for k in range(0, _v + 1):
        for i in range(0, _v + 1):
            for j in range(0, _v + 1):
                _dp[i][j] = min(_dp[i][j], _dp[i][k] + _dp[k][j])

    return _dp


if __name__ == '__main__':
    while True:
        n = int(input())
        if n == 0: break
        dp = [[100000] * 10 for i in range(10)]
        v = 0
        for i in range(0, n):
            dp[i][i] = 0
            inputs = list(map(int, input().split()))
            dp[inputs[0]][inputs[1]] = inputs[2]
            dp[inputs[1]][inputs[0]] = inputs[2]
            if v < inputs[0]:
                v = inputs[0]
            if v < inputs[1]:
                v = inputs[1]

        dp = solve(dp, v)
        ans_town = 0
        ans_dis = float("inf")
        for i in range(0, v + 1):
            tmp_sum = 0
            for j in range(0, v + 1):
                tmp_sum += dp[i][j]
            if tmp_sum < ans_dis:
                ans_dis = tmp_sum
                ans_town = i

        print(str(ans_town) + " " + str(ans_dis))

Python: import sys
readline = sys.stdin.readline
write = sys.stdout.write

INF = 10**9
def solve():
    N = int(readline())
    if N == 0:
        return False
    K = 0
    E = [[INF]*10 for i in range(10)]
    for i in range(N):
        a, b, c = map(int, readline().split())
        E[a][b] = E[b][a] = c
        K = max(K, a+1, b+1)
    for i in range(K):
        E[i][i] = 0
    for k in range(K):
        for i in range(K):
            for j in range(K):
                E[i][j] = min(E[i][j], E[i][k] + E[k][j])
    k = -1; s = INF
    for i in range(K):
        Ei = E[i]
        v = sum(Ei[j] for j in range(K) if Ei[j] < INF)
        if v < s:
            s = v; k = i
    write("%d %d\n" % (k, s))
    return True
while solve():
    ...

Python: import sys
from sys import stdin
input = stdin.readline

road = int(input())
while road > 0:
    answer = [[float("inf") if n != m else 0 for n in range(10)] for m in range(10)]
    city = 0
    for _ in range(road):
        c1, c2, w = (int(n) for n in input().split(" "))
        answer[c1][c2] = w
        answer[c2][c1] = w
        city = max(city, c1, c2)
    city += 1
    for i in range(city):
        for j in range(city):
            for k in range(city):
                answer[j][k] = min(answer[j][k], answer[j][i] + answer[i][k])
    sum_ans = [-1 , float("inf")]
    for i in range(city):
        if sum(answer[i][:city]) < sum_ans[1]:
            sum_ans = [i, sum(answer[i][:city])]
    print(*sum_ans)
    road = int(input())