#include "iostream"
#include "stdio.h"
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <string.h>
#include <stack>
#include <ctype.h>
#include <functional>
#include <math.h>
using namespace std;
//1154
int n,size,cn,d;
char c1,c2;
char t[127];
struct edge{
char fo,to;
int dis;
bool operator < (const edge &a)const {
return dis<a.dis;
}
}edge[1000];
char findRoot(char a){
if(t[a]==a) return a;
else{
char tmp=findRoot(t[a]);
t[a]=tmp;
return tmp;
}
}
int main(){
//freopen("input.txt","r",stdin);
while(cin>>n && n!=0 ){
n--;size=0;
while(n--){
cin>>c1;
cin>>cn;
if(cn==0) continue;
while(cn--){
cin>>c2>>d;
edge[size].fo=c1;
edge[size].to=c2;
edge[size++].dis=d;
}
}
sort(edge,edge+size);
for(int i='A';i<='Z';i++)
t[i]=i;
int res=0;
for(int i=0;i<size;i++){
c1=findRoot(edge[i].fo);
c2=findRoot(edge[i].to);
if(c1!=c2){
t[c1]=c2;
res+=edge[i].dis;
}
}
cout<<res<<endl;
}
return 0;
}
/**************************************************************
Problem: 1154
User: cust123
Language: C++
Result: Accepted
Time:0 ms
Memory:1528 kb
****************************************************************/
题目1154:Jungle Roads
最新推荐文章于 2020-10-11 14:32:37 发布