/*kruskal的模板,表示建图那里稍微有一点点麻烦, 我用的map建图,忘记了只有27个字母,也可以用ascii码来建图。*/ #include<stdio.h> #include<string.h> #include<map> #include<string> #include<algorithm> using namespace std; #define maxn 27 int fa[maxn]; int rank[maxn]; struct node { int st,int nd,int ln; }edge[maxn*maxn]; int cmp(node a ,node b) { return a.ln<b.ln; } int findfather(int x) { if(x!=fa[x]) fa[x]=findfather(fa[x]); return fa[x]; } void init() { int i; for(i=0;i<maxn;i++) { fa[i]=i; rank[i]=1; } } void kruskal(int n,int w) { int num,ans,i; int x,y; sort(edge,edge+w,cmp); ans=num=0; for(i=0;i<w;i++) { x=findfather(edge[i].st); y=findfather(edge[i].nd); if(x!=y) { ans+=edge[i].ln; if(rank[x]>rank[y]) { fa[y]=x; rank[x]+=rank[y]; } else { fa[x]=y; rank[y]+=rank[x]; } if(++num==n-1) break; } } printf("%d\n",ans); } int main() { char st[5],nd[5]; map<string,int>re; int n,count,k,i,j,ln,w; while(scanf("%d",&n)!=EOF&&n) { init(); re.clear(); count=0; w=0; for(i=1;i<n;i++) { scanf("%s",st); if(re[st]==0) re[st]=count++; scanf("%d",&k); for(j=1;j<=k;j++) { scanf("%s%d",nd,&ln); if(re[nd]==0) re[nd]=count++; edge[w].st=re[st]; edge[w].nd=re[nd]; edge[w].ln=ln; w++; } } kruskal(n,w); } return 0; }