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:
    try:
        n = int(input())
        if not n : break
        town = 0
        dis = [[float('inf') for j in range(10)] for i in range(10)]
        for i in range(n):
            a,b,c = map(int , input().split())
            dis[a][b] = c
            dis[b][a] = c
            town = max(town,a)
            town = max(town,b)
        for i in range(town+1):
            dis[i][i] = 0
        mindis = dis

        for i in range(town+1):
            for j in range(town+1):
                for k in range(town+1):
                    if (mindis[j][k] > mindis[j][i]+mindis[i][k]):
                        mindis[j][k] = mindis[j][i]+mindis[i][k]
        

        mintown = 0
        ans = float('inf')

        for i in range(town+1):
            sum = 0
            for j in range(town+1):
                sum += mindis[i][j]
            if (ans > sum) :
                ans = sum
                mintown = i

        print(mintown , ans)

    except:
        break

Python: num_of_path = int(input())

while num_of_path != 0:
	inf = 1e8
	data = []
	num_of_cities = 0
	for _ in range(num_of_path):
		add_data = list(map(int,input().split()))
		data.append(add_data)
		if num_of_cities < max(add_data[0],add_data[1]):
			num_of_cities = max(add_data[0],add_data[1])

	num_of_cities += 1
	w = [[1e8]*num_of_cities for i in range(num_of_cities)]
	for d in data:
		w[d[0]][d[1]] = d[2]
		w[d[1]][d[0]] = d[2]


	for k in range(num_of_cities):
		for j in range(num_of_cities):
			for i in range(num_of_cities):
				w[i][j] = min(w[i][j],w[i][k]+w[k][j])

	for i,row in zip(range(num_of_cities),w):
		row[i] = 0		

	s = [sum(w[i]) for i in range(num_of_cities)]
	print(f"{s.index(min(s))} {min(s)}")

	num_of_path = int(input())

Python: from collections import defaultdict
road = int(input())
while road > 0:
    connect = defaultdict(list)
    # weight = defaultdict(defaultdict(float("inf")))
    answer = [[float("inf") if n != m else 0 for n in range(10)] for m in range(10)]
    for _ in range(road):
        c1, c2, w = (int(n) for n in input().split(" "))
        connect[c1].append(c2)
        connect[c2].append(c1)
        answer[c1][c2] = w
        answer[c2][c1] = w
    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: ans_queue = []

while True :
    M = int(input())

    if M == 0 :
        for a in ans_queue :
            print(a[0], a[1])
        break

    N = 10
    INF = 10**20
    paths = [[0 if x == y else INF for x in range(N)] for y in range(N)]
    for i in range(M) :
        a, b, c = map(int, input().split())

        paths[a][b], paths[b][a] = c, c 
        
    for k in range(N) :
        for i in range(N) :
            for j in range(N) :
                paths[i][j] = min(paths[i][j], paths[i][k] + paths[k][j])
                
    sum_min = INF * 10
    sum_min_at = -1

    for i in range(N) :
        sum_over = 0

        if sum(paths[i]) != INF * (N - 1) : 
            for j in range(N) :
                if paths[i][j] != INF :
                    sum_over += paths[i][j]

            if sum_min > sum_over :
                sum_min = sum_over
                sum_min_at = i

    ans_queue.append((sum_min_at, sum_min))

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
            if a > max_town:
                max_town = a
            if b > max_town:
                max_town = b

        warshallFloyd(max_town + 1, dp)


        min_dist = float('inf')
        town_live = -1
        for i, d in enumerate(dp[:max_town+1]):
            sum_dist = sum(d[:max_town+1])
            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: 

while True:
    n = int(input())
    if n == 0:
        break
    AM = [[10000 ]*10 for i in range(10)]
    for i in range(10):
        AM[i][i] = 0
    maxNum = -1
    for i in range(n):
        a, b,c = map(int,input().split())
        AM[a][b] = c
        AM[b][a] = c
        maxNum = max(maxNum,a,b)
    # print(AM[:maxNum][:maxNum])
    # print(maxNum)
    sumDistance = [0]*(maxNum+1)
    for i in range(maxNum+1):
        for j in range(maxNum+1):
            for k in range(maxNum+1):
                AM[i][j] = min(AM[i][j],AM[i][k]+AM[k][j])
            sumDistance[i] += AM[i][j]
    # print(AM)
    # print(sumDistance)
    tmp = min(enumerate(sumDistance), key=lambda x: x[1])
    print(tmp[0],tmp[1])

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)]
    for _ in range(road):
        c1, c2, w = (int(n) for n in input().split(" "))
        answer[c1][c2] = w
        answer[c2][c1] = w
    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())