题意:最小生成树模板题,主要是输入方面的处理,数据不大,可以采用cin输入,也可以用scanf用getchar()来控制即可;
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<cstring>
using namespace std;
typedef struct node{
int x;int y;int date;
}node;
bool cmp(node b,node c){
return b.date<c.date;
}
int first[38];int next[38];
void csh(int m){
for(int i=1;i<=m;i++){
first[i]=i;
}
for(int i=1;i<=m;i++){
next[i]=1;
}
return ;
}
int find(int x){
if(first[x]==x) return x;
else{
x=first[x];
return find(x);
}
}
void hebin(int x,int y){
x=find(x);
y=find(y);
if(x==y) return ;
else{
if(next[x]>next[y]){
first[y]=x;
}
else{
first[x]=y;
if(next[x]==next[y]){
next[y]++;
}
}
}
return ;
}
node a[500];
int main(){
int n;
while(cin>>n&&n!=0){
int k=0;
for(int i=1;i<=n-1;i++){
char ch;int p;
cin>>ch>>p;
char ch1;int w;
for(int j=1;j<=p;j++){
cin>>ch1>>w;
k++;
a[k].x=(ch-'A'+1);
a[k].y=(ch1-'A'+1);
a[k].date=w;
}
}
csh(n);
sort(a,a+k+1,cmp);
int sum=0;
int l=0;
for(int i=1;i<=k;i++){
if(find(a[i].x)!=find(a[i].y)){
sum+=a[i].date;
l++;
hebin(a[i].x,a[i].y);
if(l==(n-1)){
break;
}
}
}
cout<<sum<<endl;
}
return 0;
}