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. WinForm一次只打开一个程序

    WinForm如果我们希望一次只打开一个程序,那么我们在程序每次运行的时候都需要检测线程是否存在该程序,如果存在就呼出之前的窗体,C#代码如下: using System; using System. ...

  2. 关于c#除法运算的问题

    https://blog.csdn.net/yxt1522916229/article/details/51107569/ 下面的示例可以验证一下问题: 例如: int m = 2;         ...

  3. android--------自定义控件ListView实现下拉刷新和上拉加载

    开发项目过程中基本都会用到listView的下拉刷新和上滑加载更多,为了方便重写的ListView来实现下拉刷新,同时添加了上拉自动加载更多的功能. Android下拉刷新可以分为两种情况: 1.获取 ...

  4. week 1

    day1 订正 学习AC自动机 day2 mobius反演 对偶图 codeforces day3 ZR模拟赛 订正 day4 复习AC自动机 题库 https://www.cnblogs.com/c ...

  5. Dajngo的CBV和FBV

    CBV: class. base. view 路由: url(r'students/', views.StudentsView.as_view()) 视图: from django.views imp ...

  6. 检测Linux glibc幽灵漏洞和修补漏洞

    1.首先安装rpm : sudo apt-get install rpm   wget -OGHOST-test.sh http://www.antian365.com/lab/linux0day/G ...

  7. 悟空CRM框架下载模板

    1.你可以把你要下载的模板放在这个项目的Public/excelmodel目录下,然后你就可以在页面中编写代码 你可以在下载按钮这里编写:onclick="window.location.h ...

  8. CF-499div2-E-裴蜀定理

    E. Border time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  9. cf188C(最大子段和&&思维)

    C. Functions again time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  10. Apollo配置中心介绍

    参考链接:https://github.com/ctripcorp/apollo/wiki/Apollo%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%E4%BB%8B%E7 ...