Jungle Roads


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.

Input

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. 

Output

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. 

Sample Input

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

Sample Output

216
30 题意:经典最小生成树。
思路:有向边无向边对生成树来说不重要。顺序读入即可。这里有个小技巧,scanf里的空格可以吃掉连续一串(可以为空)空白符(空格,tab,换行),直到出现第一个非空白符,比getchar()更适用。
#include<stdio.h>
#include<algorithm>
using namespace std; int f[],a[];
struct Edge{
int u,v,w;
}edge[]; bool cmp(Edge a,Edge b)
{
return a.w<b.w;
} int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
} int kru(int c,int n)
{
int i;
for(i=;i<=n;i++){
f[i]=i;
}
sort(edge+,edge+c+,cmp);
int cnt=,ans=;
for(i=;i<=c;i++){
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int fu=find(u),fv=find(v);
if(fu!=fv){
ans+=w;
f[fv]=fu;
cnt++;
}
if(cnt==n-) break;
}
if(cnt<n-) return -;
else return ans;
}
int main()
{
int n,x,t,c,i,j;
char ch1,ch2;
while(scanf("%d",&n)&&n!=){
c=;
for(i=;i<n;i++){
scanf(" %c %d",&ch1,&t); //
for(j=;j<=t;j++){
scanf(" %c %d",&ch2,&x); //getchar()个别出现RE情况
edge[++c].u=ch1-'A'+;
edge[c].v=ch2-'A'+;
edge[c].w=x;
}
}
printf("%d\n",kru(c,n));
}
return ;
}

POJ1251 Jungle Roads Kruskal+scanf输入小技巧的更多相关文章

  1. POJ1251 Jungle Roads(Kruskal)(并查集)

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23882   Accepted: 11193 De ...

  2. poj1251 Jungle Roads Kruskal算法+并查集

    时限: 1000MS   内存限制: 10000K 提交总数: 37001   接受: 17398 描述 热带岛屿拉格里山的首长有个问题.几年前,大量的外援花在了村庄之间的额外道路上.但是丛林不断地超 ...

  3. HDU1301&&POJ1251 Jungle Roads 2017-04-12 23:27 40人阅读 评论(0) 收藏

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25993   Accepted: 12181 De ...

  4. POJ1251 Jungle Roads 【最小生成树Prim】

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19536   Accepted: 8970 Des ...

  5. POJ1251 Jungle Roads (最小生成树&Kruskal&Prim)题解

    题意: 输入n,然后接下来有n-1行表示边的加边的权值情况.如A 2 B 12 I 25 表示A有两个邻点,B和I,A-B权值是12,A-I权值是25.求连接这棵树的最小权值. 思路: 一开始是在做莫 ...

  6. poj1251 Jungle Roads(Prime || Kruskal)

    题目链接 http://poj.org/problem?id=1251 题意 有n个村庄,村庄之间有道路连接,求一条最短的路径能够连接起所有村庄,输出这条最短路径的长度. 思路 最小生成树问题,使用普 ...

  7. POJ1251 Jungle Roads【最小生成树】

    题意: 首先给你一个图,需要你求出最小生成树,首先输入n个节点,用大写字母表示各节点,接着说有几个点和它相连,然后给出节点与节点之间的权值.拿第二个样例举例:比如有3个节点,然后接下来有3-1行表示了 ...

  8. HDU1301 Jungle Roads(Kruskal)

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

  9. POJ1251 Jungle Roads

    解题思路:看懂题意是关键,Kruskal算法,最小生成树模板. 上代码: #include<cstdio> #include<cstring> #include<algo ...

随机推荐

  1. 我的Android进阶之旅------>Android如何去除GridView的按下或点击选中后的背景效果

    今天用GridView做了一个界面,自己自定好了一个组件,并且设置好了点击和不点击组件时候的效果,但是运行的时候发现在我定义好的背景下面还有一层不知道哪儿来的背景,严重影响了我自定义的组件的效果. 后 ...

  2. SpringBoot学习笔记(13):日志框架

    SpringBoot学习笔记(13):日志框架——SL4J 快速开始 说明 SpringBoot底层选用SLF4J和LogBack日志框架. SLF4J的使用 SpringBoot的底层依赖关系 1. ...

  3. 7-6 公路村村通(30 分) 【prime】

    7-6 公路村村通(30 分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤10 ...

  4. c# wpf ComboBox 动态下拉框 及 动态默认值设定

    1.下拉框声明 <ComboBox x:Name="DirComboBox" Width="150" Height="18" Marg ...

  5. Linux常用命令全集

    一,安装和登陆命令1,进入图形界面 startx 2,进入图形界面 init 5 3,进入字符界面 init 34,登陆 login 5,关机 poweroff -p 关闭机器的时候关闭电源-n 在关 ...

  6. Python中出现“TabError: inconsistent use of tabs and spaces in indentation”问题的解决

  7. malloc和new的区别是什么?

    http://zhidao.baidu.com/link?url=iUDUZeJtj1o12PvUETLlJgvAMqzky5HxGCJRGnULpsO8HdWAdjKkQqGCJ9-o-aTu8NP ...

  8. MyBatis映射文件中用#和$传递参数的特点

    在MyBatis映射文件中用#和$传递参数的特点, #是以占位符的形式来传递对应变量的参数值的,框架会对传入的参数做预编译的动作, 用$时会将传入的变量的参数值原样的传递过去,并且用$传递传递参数的时 ...

  9. Java企业微信开发_10_未验证域名归属,JS-SDK功能受限

    1.现象: 在企业微信后台填写可信域名后,提示:未验证域名归属,JS-SDK功能受限,如下图: 点击“申请域名校验”后, 注意:域名根目录 当时一直不清楚这个域名根目录在哪里,最后让我给试出来了 2. ...

  10. String类中的equals是如何重写的

    我们知道String中的equals方法是被重写过的,因为object的equals方法是比较的对象的内存地址,而String的equals方法比较的是对象的值. 首先几个知识点: 基本数据类型==比 ...