枚舉起點。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
#define max(a,b) (((a) > (b)) ? (a) : (b))
using namespace std;
const int MAX = 100;
const int SUP = 999999999;
int find_max(int *begin, int *end)
{
int ret = -SUP;
int *p = begin;
while (p != end)
{
ret = max(ret, *p);
p++;
}
return ret;
}
struct Road
{
int des;
int cost;
Road(int des=0, int cost=0)
{
this->des = des;
this->cost = cost;
}
};
vector<Road> road[MAX+2];
int n;
int ans;
int start;
int pass[MAX+2];
bool visit[MAX+2];
queue<int> q;
void read_map()
{
for (int i = 1; i <= n; i++)
road[i].clear();
for (int i = 1; i <= n; i++)
{
int sz;
scanf("%d", &sz);
for (int j = 1; j <= sz; j++)
{
int des, cost;
scanf("%d%d", &des, &cost);
road[i].push_back(Road(des, cost));
}
}
}
void _bfs(int x)
{
for (int i = 1; i <= n; i++)
pass[i] = SUP;
while (!q.empty())
q.pop();
memset(visit, 0, sizeof(visit));
int head;
int cnt = 0;
q.push(x);
pass[x] = 0;
while (!q.empty())
{
head = q.front();
q.pop();
if (!visit[head])
{
visit[head] = true;
cnt++;
}
int sz = road[head].size();
for (int i = 0; i < sz; i++)
{
if (pass[road[head][i].des] > pass[head] + road[head][i].cost)
{
pass[road[head][i].des] = pass[head] + road[head][i].cost;
q.push(road[head][i].des);
}
}
}
if (cnt == n)
{
int max_cost = find_max(pass+1, pass + n+1);
if (max_cost < ans)
{
ans = max_cost;
start = x;
}
}
}
void _print()
{
if (ans != SUP)
printf("%d %d\n", start, ans);
else
printf("disjoint\n");
}
int main()
{
while (scanf("%d", &n) && n)
{
read_map();
ans = SUP;
for (int i = 1; i <= n; i++)
_bfs(i);
_print();
}
return 0;
}