题目描述(已转换成中文)
有一个旅游区,旅游区有很多的景点,景点间需要开通缆车,使得任意两个景点可以互相到达。现在给出一些点间的缆车线路制造成本,两个景点之间可能有多重制造方式。问最少的花费是多少。
输入格式
输入包括多组数据,最多100组,最后以输入0表示结束。对于每一组数据,第一行一个n,表示景区数量,1<n<27。
之后n-1行,每行第一个是一个大写字母,表示当前景区的编号,接着是k,表示当前景区对外可以有k中缆车线路制造方式,接着k对,每对第一个是一个字母,表示这条路线连接的景区,第二个是一个数字,表示制造成本。0<=k<=15。最多75条制造线路,每条线路的制造成本最大是100。
输出格式
对于每一组数据,输出一行表示最小成本。
输入输出样例
输入
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
输出
216
30
题目链接
分析:
这道题需要注意的地方:
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
typedef long long LL;
int n, tot, ans, fu[30];
char u[3], x[3];
struct node{
int from;
int to;
int w;
}a[80];
void init(){
tot = 0;
ans = 0;
for(int i = 0; i < n; i++) fu[i] = i;
}
int find(int x){
if(fu[x] == x)return fu[x];
return fu[x] = find(fu[x]);
}
bool cmp(node x, node y){
return x.w < y.w;
}
void kruskal(){
sort(a, a + tot, cmp);
for(int i = 0; i < tot; i++){
if(find(a[i].from) != find(a[i].to)){
ans += a[i].w;
fu[find(a[i].from)] = find(a[i].to);
}
}
}
int main(){
int m, i, j, t, w;
while(scanf("%d", &n) && n){
init();
for(i = 0; i < n - 1; i++){
scanf("%s%d", u, &m);
t = u[0] - 'A';
for(j = 0; j < m; j++){
scanf("%s%d", u, &w);
a[tot].from = t;
a[tot].to = u[0] - 'A';
a[tot++].w = w;
}
}
kruskal();
printf("%d\n", ans);
}
return 0;
}