题意:用桥把n个岛屿连接起来
直接最小生成树就行
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define maxn 200009
using namespace std;
int n, m, tt, father[1009];
struct cadongllas
{
int x, y, value;
}t[100];
bool cmp (cadongllas x, cadongllas y)
{
return x.value < y.value;
}
int get_father (int x)
{
if (father[x] == x)
return x;
return get_father (father[x]);
}
int main ()
{
while (scanf ("%d", &n) == 1 && n)
{
int ans = 0, cnt = 0;
rep (i, 1, n - 1)
{
char s[10];
scanf ("%s", s);
scanf ("%d", &m);
rep (j, 1, m)
scanf ("%s%d", s, &tt), t[++cnt] = (cadongllas){i, s[0] - 'A' + 1, tt};// printf ("add %d %d --%d\n", i, s[0] - 'A' + 1, tt);
}
sort (t + 1, t + 1 + cnt, cmp);
rep (i, 1, n)
father[i] = i;
rep (i, 1, cnt)
{
int u = get_father (t[i].x), v = get_father (t[i].y);
if (u != v)
father[u] = v, ans += t[i].value;
}
cout << ans << endl;
}
return 0;
}