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: 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())

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[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: # Aizu Problem 0189: Convenient Location
#
import sys, math, os, bisect

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")



while True:
    n = int(input())
    if n == 0:
        break
    L = []
    cities = set()
    for _ in range(n):
        city1, city2, weight = [int(_) for _ in input().split()]
        L.append([city1, city2, weight])
        cities.add(city1)
        cities.add(city2)
    m = len(cities)
    dist = [[2**31] * m for _ in range(m)]
    for i in range(m):
        dist[i][i] = 0
    for city1, city2, weight in L:
        dist[city1][city2] = weight
        dist[city2][city1] = weight
    for k in range(m):
        for i in range(m):
            for j in range(m):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    city, time = -1, 2**31
    for k in range(m):
        S = sum(dist[k])
        if S < time:
            time = S
            city = k
    print(city, time)

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)
    city = max(connect.keys()) + 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())

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: # -*- 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: 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 i in range(10):
    for j in range(10):
      for k 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)