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

Python: def floyd_warshall(d):
    v = len(d)
    for k in range(v):
        for i in range(v):
            for j in range(v):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])

while True:
    n = int(input())
    if n == 0:
        break
    info = []
    num_cities = 1
    for i in range(n):
        a, b, c = map(int, input().split())
        num_cities = max(num_cities, a+1, b+1)
        info.append((a, b, c))

    d = [[0 if i == j else float('inf') for j in range(num_cities)] for i in range(num_cities)]
    for a, b, c in info:
        d[a][b] = d[b][a] = c

    floyd_warshall(d)
    total = [sum(d[i]) for i in range(num_cities)]
    min_dist = min(total)
    print(total.index(min_dist), min_dist)

Python: INF = 10000000

while True:
    n = int(input())
    if n == 0:
        break
    
    table = [[INF for i in range(10)] for j in range(10)]
    
    for l in range(n):
        a,b,c = [int(i) for i in input().split()]
        table[a][b] = c
        table[b][a] = c
        

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

    ans = INF
    ansid = 0
    for i in range(10):
        tmp = 0
        flag = False
        for j in range(10):
            if table[i][j] < INF and i != j:
                tmp = tmp + table[i][j]
                flag = True
        if tmp < ans and flag:
            ans = tmp
            ansid = i

    print(ansid,ans)

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: import sys
import math
import fractions
import itertools
from collections import deque
import copy
import bisect
import heapq

MOD = float('inf')
INF = 10 ** 18 + 7
input = lambda: sys.stdin.readline().strip()


def warshall_floyd(d):
    """
    d[i][l]がi→lへのコストを示す隣接行列
    結果としてd[i][l]はi→lへの最短コストになる。
    """
    for i in range(len(d)):
        for l in range(len(d)):
            for k in range(len(d)):
                d[l][k] = min(d[l][k], d[l][i] + d[i][k])
    return d


while True:
    n = int(input())
    if n == 0:
        break
    else:
        abc = [list(map(int, input().split())) for _ in range(n)]
        info = [[INF for _ in range(10)] for _ in range(10)]
        for i in range(10):
            info[i][i] = 0
        for i in abc:
            info[i[0]][i[1]] = i[2]
            info[i[1]][i[0]] = i[2]
        result = warshall_floyd(info)
        ans = []
        for i in result:
            for l in range(len(i)):
                if i[l] == INF:
                    i[l] = 0
        for i in range(10):
            if sum(result[i]) != 0:
                ans.append([sum(result[i]), i])
        ans.sort()
        print(ans[0][1], ans[0][0])

C++: #include <bits/stdc++.h>
using namespace std;

const int maxn = 45 + 15;
const int INF = 1e7;
int n, dis[maxn][maxn], cns;
map<int, int> cache;
vector<int> Find;
void init(){
	cache.clear(), Find.clear(), cns = 0;
	Find.push_back(0);
	for(int i = 1; i < maxn; i++)
	for(int j = 1; j < maxn; j++){
		if(i == j) dis[i][j] = 0;
		else dis[i][j] = dis[j][i] = INF;
	}
}
int ID(int x){
	if(cache.count(x)) return cache[x];
	Find.push_back(x);
	return cache[x] = ++cns;
} 
void floyd_warshall(){
	for(int k = 1; k <= cns; k++)
	for(int i = 1; i <= cns; i++)
	for(int j=1;j<=cns;j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}

int main(){
	while(scanf("%d", &n) == 1&& n){
		init();
		int ori, fin, val;
		while(n--){
			scanf("%d %d %d", &ori, &fin, &val);
			int xx = ID(ori), yy = ID(fin);
			dis[xx][yy] = dis[yy][xx] = val;
		}
		floyd_warshall();
		int ans = INF, index = -1;
		for(int i = 1; i <= cns; i++){
			int tem = 0;
			for(int j=1;j<=cns;j++) tem+=dis[i][j];
			if(tem < ans) ans = tem, index = i;
		} 
		printf("%d %d\n", Find[index], ans);
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>

using namespace std;

const int INF=1<<21;

int main()
{
	int n;
	while(cin>>n,n)
	{
		int m[10][10]={};
		for(int i=0;i<10;i++)for(int j=0;j<10;j++)if(i!=j)m[i][j]=INF;
		int a,b,w,maxn=0;
		for(int i=0;i<n;i++)
		{
			cin>>a>>b>>w;
			m[a][b]=m[b][a]=w;
			maxn=max(maxn,max(a,b));
		}
		maxn++;
		for(int k=0;k<maxn;k++)
			for(int i=0;i<maxn;i++)
				for(int j=0;j<maxn;j++)
					m[i][j]=min(m[i][j],m[i][k]+m[k][j]);
		
		int d[10];
		for(int i=0;i<maxn;i++)d[i]=0;
		for(int i=0;i<maxn;i++)
		{
			for(int j=0;j<maxn;j++)
			{
				d[i]+=m[i][j];
			}
		}
		int mini=0,mind=INF;
		for(int i=0;i<maxn;i++)
		{
			if(mind>d[i])
			{
				mini=i;
				mind=d[i];
			}
		}
		cout<<mini<<" "<<d[mini]<<endl;;
	}
	return 0;
}