题意分析:
模板题了吧。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define MAX 30
struct edge { int u, v, cost; };
bool cmp(const edge& e1, const edge& e2) { return e1.cost < e2.cost; }
vector<edge>es;//边集合
int fa[MAX];//并查集
int n;//顶点数
int find(int x) { return fa[x] == -1 ? x : fa[x] = find(fa[x]); }//并查集查找 路径压缩
int kk()
{int cnt = 0;sort(es.begin(), es.end(), cmp);//对边进行排序memset(fa, -1, sizeof(fa));//并查集初始化int res = 0;for (int i = 0; i < es.size(); i++){if (find(es[i].u) != find(es[i].v))//不在同一个连通分量{fa[find(es[i].u)] = find(es[i].v);//uniteres += es[i].cost;}}return res;
}int main()
{int n;while (cin >> n,n){es.clear();for (int i = 0; i < n - 1; i++){char c1, c2; int k,v,d;cin >> c1 >> k;while (k--){cin >> c2 >> d;v = c2 - 'A';edge tmp = { i,v,d };es.push_back(tmp);}}cout << kk() << endl;}system("pause");
}