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
解题思路:
大致题意:在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使每个岛屿都间接或直接与其他岛屿相连时所用的的最短时间(只有修完一个桥后才可修下一个桥)。
简言之就是求最小生成树。
对于数据,数据输入的第一行n代表岛屿的个数,当为0是结束程序,接着n-1行开始时为这岛屿的编号,用大写字母表示,接着是一个整数m
代码如下:
 #include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std; int n , m ,k;
char a , b ;
struct edge{
int u ;
int v ;
int w ;
}e[];
int pre[];
bool cmp(edge a ,edge b)
{
return a.w < b.w;
}
int find(int x)
{
return (x==pre[x])?x:pre[x] = find(pre[x]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
int edge_num = ;
for(int i = ; i <= ;i++)
{
pre[i] = i;
} for(int i = ; i < n ;i++)
{
cin>>a>>m;
for(int j = ; j < m ; j++)
{
cin>>b>>k;
e[edge_num].u = a ;
e[edge_num].v = b ;
e[edge_num].w = k;
edge_num++;
}
}
int ans = ;
sort(e,e+edge_num,cmp);
for(int i = ; i < edge_num ;i++)
{
int u = e[i].u;
int v = e[i].v;
int w = e[i].w;
int fx = find(u);
int fy = find(v);
if(fx!=fy)
{
pre[fx] = fy;
ans += w;
}else
{
continue;
}
}
printf("%d\n",ans);
}
return ;
}

POJ - 1251A - Jungle Roads 利用最小生成树的更多相关文章

  1. POJ 1251 Jungle Roads(最小生成树)

    题意  有n个村子  输入n  然后n-1行先输入村子的序号和与该村子相连的村子数t  后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离  求链接全部村子的最短路径 还是裸的最小生成树咯 ...

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

    题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...

  3. POJ - 1251 Jungle Roads (最小生成树&并查集

    #include<iostream> #include<algorithm> using namespace std; ,tot=; const int N = 1e5; ]; ...

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

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

  5. POJ 1251 && HDU 1301 Jungle Roads (最小生成树)

    Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...

  6. POJ 1251 Jungle Roads - C语言 - Kruskal算法

    Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...

  7. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

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

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

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

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

随机推荐

  1. spring与mybatis

  2. MySQL数据库篇之完整性约束和表关系

    主要内容: 一.完整性约束 二.表关系 1️⃣ 完整性约束 (1)何为完整性约束? 约束条件与数据类型的宽度一样,都是可选参数. 作用:用于保证数据的完整性和一致性 (2)分类主要有以下五类: 1.n ...

  3. 微信公众号token 验证

    1. 首先给出测试项目的整体目录: 2. CoreServlet类: 当get请求的时候会执行get方法,post请求的时候会执行post方法,分别来处理不同的请求 package com.zjn.s ...

  4. ActiveMQ集群整体认识

    出自:https://segmentfault.com/a/1190000014592517 前言 最终需要掌握 Replicated LevelDB Store部署方式,这种部署方式是基于ZooKe ...

  5. C#自省

    [C#自省] 1.根据string,获取type.Type.GetType 方法,获取具有指定名称的 Type,执行区分大小写的搜索. 2.根据obj,获取type.Object.GetType 方法 ...

  6. Codeforces 703E DP + 因数分解 +离散化

    题意:给你n个数,和一个数m, 问最小需要多少个数,可以让这些数乘起来是m的倍数.如果有多组,取和最小的那一组. 思路:因为m的范围到1e12,并且和取模相关,所以容易想到处理出m的约数,然后离散化一 ...

  7. ubuntu16.04 安装opencv3

    (opencvC++) luo@luo-ThinkPad-W540:20181205$ conda install --channel https://conda.anaconda.org/menpo ...

  8. 对比Nginx配置文件差异

    一.概要: Python2 官方文档:https://docs.python.org/2/library/difflib.html Python2 官方文档:https://docs.python.o ...

  9. CecOS Virtualization

    CecOS CecOS 是 OPENFANS 为中小型企业提供企业开源云计算解决方案的基础框架:CecOSvt 是一款运行在社区企业云操作系统(CecOS)上的虚拟化开放式解决方案. 也可用演示模式. ...

  10. 10个android开发必备的开源项目

    You are here: Home » » Blog » 10 Open Source Android Apps which every Android developer must look in ...