The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

其实就是最小生成树裸题

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int fa[],n; struct seg{
int a,b,l;
}s[]; bool cmp(seg a,seg b){
return a.l<b.l;
} void init(){
int i;for(i=;i<=n;i++)fa[i]=i;
} int find(int x){
if(fa[x]==x)return x;
fa[x]=find(fa[x]);
return fa[x];
} void unio(int x,int y){
int dx=find(x),dy=find(y);
if(dx!=dy)fa[dx]=dy;
} int main(){
while(scanf("%d",&n)!=EOF&&n!=){
char a[],b[];
int i,t,c=;
memset(s,,sizeof(s));
for(i=;i<n;i++){
scanf("%s%d",a,&t);
int v;
for(int j=;j<=t;j++){
scanf("%s%d",b,&v);
s[++c].a=a[]-'A'+;
s[c].b=b[]-'A'+;
s[c].l=v;
}
}
init();
sort(s+,s+c+,cmp);
int ans=;t=;
for(i=;i<=c;i++){
if(find(s[i].a)==find(s[i].b))continue;
ans+=s[i].l;
unio(s[i].a,s[i].b);
t++;
if(t==n-)break;
}
printf("%d\n",ans);
}
return ;
}

hdu1301 Jungle Roads 最小生成树的更多相关文章

  1. HDU-1301 Jungle Roads(最小生成树[Prim])

    Jungle Roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  2. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  3. HDU1301 Jungle Roads

    Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...

  4. hdu1301 Jungle Roads 基础最小生成树

    #include<iostream> #include<algorithm> using namespace std; ; int n, m; ]; struct node { ...

  5. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

  6. hdu Jungle Roads(最小生成树)

    Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of for ...

  7. HDU1301 Jungle Roads(Kruskal)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  8. hdu1301 Jungle Roads (Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 依旧Prim............不多说了 #include<iostream> ...

  9. hduoj-1301 Jungle Roads(最小生成树-克鲁斯卡尔和普里姆求解)

    普里姆求解: #include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...

随机推荐

  1. 雷林鹏分享:C# 反射(Reflection)

    C# 反射(Reflection) 反射(Reflection) 对象用于在运行时获取类型信息.该类位于 System.Reflection 命名空间中,可访问一个正在运行的程序的元数据. Syste ...

  2. Isotig & cDNA & gene structure & alternative splicing & gene loci & 表达谱

    参考:高通量测序相关名词 Isotig 指在转录组de novo测序时,用454平台测序完成后组装出的结果,一个isotig可视为一个转录本. Isogroup 指转录组de novo测序中,用454 ...

  3. 12月10日 render( locals:{...}) 传入本地变量。

    Jdstor第一部分后台设计,4-4上传图片. 3.4 Using Partials--3.4.4 Passing Local Variables You can also pass local va ...

  4. Spring web flow的意义

    为什么要使用Spring web flow呢? 这里需要强调的一点就是,但凡一个技术的出现和流行,必有其适用的环境和存在的意义. Spring web flow加强了中央集权,这个该怎么理解呢?以往我 ...

  5. Redis Commands(1)

    Redis 命令分为15类,如下: Cluster Connection Geo Hashes HyperLogLog Keys Lists Pub/Sub Scripting Server Sets ...

  6. Coconuts, Revisited(递推+枚举+模拟)

    Description The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening ...

  7. Error Code: 1175. You are using safe update mode and you tried to ......

    MySQL提示的错误信息: Error Code: 1175. You are using safe update mode and you tried to update a table witho ...

  8. Python的数据类型2列表

    Python的数值类型List,也就是列表 Python的列表比较类似与其他语言的数组概念,但他又与其他语言数组的概念有很大的不同 C语言.Java的数组定义是这样的,存储多个同类型的数值的集合就叫数 ...

  9. 快速切题 sgu105. Div 3 数学归纳 数位+整除 难度:0

    105. Div 3 time limit per test: 0.25 sec. memory limit per test: 4096 KB There is sequence 1, 12, 12 ...

  10. bzoj1626

    题解: 简单最小生成树 x,y都要double 我也不知道为什么 代码: #include<bits/stdc++.h> using namespace std; ; int n,m,f[ ...