题目链接http://poj.org/problem?id=1251
题意:
没什么意思,就是最小生成树,直接贴代码,坑在于输入。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define E 100
#define N 30
#define fp(_p,_q,_r) for(int _p=_q;_p<_r;_p++)
using namespace std;
struct edge { int u,v,c; edge(int a = 0,int b = 0,int c = 0):u(a),v(b),c(c){}};
edge es[E];
int n,e,p[E];
bool cmp(edge e1,edge e2){
return e1.c < e2.c;
}
int find(int x){
return x == p[x] ? x : p[x] = find(p[x]);
}
bool same(int x,int y){
return find(x) == find(y);
}
void unite(int x,int y){
p[p[x]] = p[y];
}
int kruskal(){
sort(es,es + e,cmp);
fp(i,0,n){
p[i] = i;
}
int res = 0;
fp(i,0,e){
edge e = es[i];
if(!same(e.u,e.v)){
unite(e.u,e.v);
res += e.c;
}
}
return res;
}
int main(void)
{
while(cin >> n && n){
memset(es,-1,sizeof(es));
int cur = 0;
fp(i,0,n-1){
getchar();
getchar();
getchar();
int t,l; char ch;
cin >> t;
while(t --){
getchar();
cin >> ch >> l;
es[cur ++] = edge(i,ch - 'A',l);
}
}
e = cur;
cout << kruskal() << endl;
}
return 0;
}