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

C++: #include<bits/stdc++.h>
using namespace std;
const int maxn = 10;
const int maxe = 100;
int ecnt;
struct Edge{
    int to;
    int val;
    int nxt;
}edge[maxe];
int node[maxn];
void init()
{
    memset(node,-1,sizeof node);
    ecnt = 0;
}
void addedge(int from,int to,int val)
{
    edge[ecnt].nxt = node[from];
    edge[ecnt].to = to;
    edge[ecnt].val = val;
    node[from] = ecnt++;
}
int d[maxn];
int V;  //结点数
const int INF = 0x3f3f3f3f;
bool bellman_ford(int s){ //返回true代表有负环
    for(int i=0;i<V;++i)    d[i]=INF;
    d[s]=0;
    bool update;int now;
    for(int i=0;i<V;++i){
        update=false;
        for(int j=0;j<V;++j){
            now=node[j];
            while(now!=-1)
            {
                Edge &e=edge[now];
                if(d[e.to]>d[j]+e.val)
                {
                    d[e.to]=d[j]+e.val;
                    update=true;
                }
                now=e.nxt;
            }
        }
        if(!update) return false;
    }
    return true;
}
bool vis[maxn];
int D[maxn];
int main()
{
    ios::sync_with_stdio(false);
    int m;
    V=10;
    while(cin>>m&&m)
    {
        init();
        int u,v,w;
        memset(vis,0,sizeof vis);
        while(m--)
        {
            cin>>u>>v>>w;
            addedge(u,v,w);
            addedge(v,u,w);
            vis[u]=vis[v]=true;
        }
        for(int i=0;i<10;++i)
        {
            if(!vis[i]) continue;
            D[i]=0;
            bellman_ford(i);
            for(int j=0;j<10;++j)
                if(vis[j])
                    D[i]+=d[j];
        }
        int sm=0;
        for(int i=1;i<10;++i)   if(vis[i]&&D[sm]>D[i])  sm=i;
        cout<<sm<<" "<<D[sm]<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <stack>
#include <queue>
#include <cmath>

#define rep(i,j,k) for(int i=j;i<k;i++)

const int INF = 100000000;

using namespace std;


int main() {
	while (1) {
		int n;
		cin >> n;
		if (n == 0)break;
		vector<vector<int>> v(10, vector<int>(10, INF));

		int maxnum=0;
		rep(i, 0, n) {
			int from, to, cost;
			cin >> from >> to >> cost;
			v[from][to] = cost;
			v[to][from] = cost;

			if (maxnum < to)maxnum = to;
			v[i][i] = 0;
		}
		maxnum++;

		rep(i, 0, maxnum) {
			rep(j, 0,maxnum) {
				rep(k, 0, maxnum) {
					v[j][k] = min(v[j][k], v[j][i] + v[i][k]);
				}
			}
		}

		/*rep(i, 0, maxnum) {
			rep(j, 0, maxnum) cout << i << " " << j << " " << v[i][j] << endl;
		}*/

		int ans = INF, ansnum;
		rep(i, 0, maxnum) {
			int sum = 0;
			rep(j, 0, maxnum) {
				if(i!=j && v[i][j]!=INF)sum += v[i][j];
			}
			if (ans > sum) {
				ans = sum;
				ansnum = i;
			}
		}

		cout << ansnum << " " << ans << endl;
	}
	
	
	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
#include <stack>
#include <queue>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()
 
#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)
#define REPE(i,s,e)	for(int i=s; i>e; i--)
#define repe(i,n)	REPE(i,n,-1)


typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<vi> vvi;
 
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007
 

const int MAX_V = 100;

int d[MAX_V][MAX_V];
int V;
 
void warshall_floyd_init(){
	for(int i = 0; i < V; i++){
		for(int j = 0; j < V; j++){
			if(i == j) d[i][i] = 0;
			else d[i][j] = INF;
		}
	}
}

void warshall_floyd() {
	for (int k = 0; k < V; k++) {
		for (int i = 0; i < V; i++) {
			for (int j = 0; j < V; j++) {
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
			}
		}
	}
}


int main(){
	int n;
	while(cin >> n && n > 0){
		V = 10;
		int a, b, c;
		warshall_floyd_init();
		V = 0;
		rep(i, n){
			cin>>a>>b>>c;
			d[a][b] = d[b][a] = c;
			if(V < a + 1) V = a + 1;
			if(V < b + 1) V = b + 1;
		}
		//cout<<V<<endl;
		warshall_floyd();
		int ans = INF, no =0;
		for(int i = 0; i < V; i++){
			int sum = 0;
			for(int j = 0; j < V; j++){
				sum += d[i][j];
			}
			//cout<<sum<<endl;
			if(sum < ans) ans = sum, no = i;
		}
		cout<<no<<" "<<ans<<endl;
	}
}

C++: #include<iostream>
using namespace std;
#define V 10
#define INF (1<<30)-1
struct edge{int from,to,cost;};
int main(){
  int d[V];
  int n;
  while(cin>>n){
    if(n==0) break;
    edge es[2*n];
    int a,b,c;
    int ans = INF,ansi;
    for(int i=0;i<2*n;i+=2){
      cin >> a >> b >> c;
      es[i].from = es[i+1].to = a;
      es[i+1].from = es[i].to = b;
      es[i].cost = es[i+1].cost = c;
    }
    for(int j=0;j<V;j++){
      for(int i=0;i<V;i++) d[i] = INF; 
      d[j] = 0;
      while(true){
	bool updata = false;
	for(int i=0;i<2*n;i++){
	  edge e = es[i];
	  if(d[e.from]!=INF&&d[e.to]>d[e.from]+e.cost){
	    d[e.to] = d[e.from] + e.cost;
	    updata = true;
	  }
	}
	if(!updata) break;
      }    
      int s = 0;
      for(int i=0;i<V;i++){
	if(d[i]!=INF){
	  s += d[i];
	}
      }
      if(s<ans&&s!=0){ans = s,ansi =j;};
    }
    cout << ansi << ' ' << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int N=50,INF=0x3f3f3f3f;
int n,G[N][N],sum[N],ans;
signed main(void){
    ios::sync_with_stdio(false);
    while(cin>>n&&n){
    	memset(sum,0,sizeof(sum));
    	memset(G,0x3f,sizeof(G));
    	for(int i=1,x,y,w;i<=n;i++,ans=x){
    		cin>>x>>y>>w;
    		G[x][x]=G[y][y]=0;
    		G[x][y]=G[y][x]=w;
    	}
    	for(int k=0;k<N;k++)
    		for(int i=0;i<N;i++)
    			for(int j=0;j<N;j++)
    				G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
    	for(int i=0;i<N;i++)
    		if(G[i][i]==0)
    			for(int j=0;j<N;j++)
    				if(G[j][j]==0)
    					sum[i]+=G[i][j];
    	for(int i=0;i<N;i++)
    		if(G[i][i]==0)
    			if(sum[ans]>sum[i])
    				ans=i;
    	cout<<ans<<" "<<sum[ans]<<endl;
    }
}