Description

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 最小生成树模板题
#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 110
#define INF 0xfffffff using namespace std; int dist[N], vis[N], n, maps[N][N]; void Init()
{
for(int i=; i<=n; i++)
{
vis[i] = ;
dist[i] = INF;
for(int j=; j<=n; j++)
{
maps[i][j] = INF;
}
}
} int Prim(int start)
{
int ans = ;
for(int i=; i<=n; i++)
dist[i] = maps[start][i];
vis[start] = ;
for(int i=; i<n; i++)
{
int Min = INF,index = -;
for(int j=; j<=n; j++)
{
if(vis[j]== && Min > dist[j])
Min = dist[j], index = j;
}
if(index == -) break;
vis[index] = ;
ans += Min;
for(int j=; j<=n; j++)
{
if(vis[j]== && dist[j] > maps[index][j])
{
dist[j] = maps[index][j];
}
}
}
return ans;
} int main()
{
int i, j, x, a, b, ans, m;
char s[];
while(scanf("%d", &n), n)
{
Init();
for(i=; i<n; i++)
{
scanf("%s%d", s, &m);
a = s[] - 'A' + ;
for(j=; j<m; j++)
{
scanf("%s%d", s, &x);
b = s[] - 'A' + ;
maps[a][b] = maps[b][a] = min(maps[a][b],x);
}
}
ans = Prim();
printf("%d\n", ans);
}
return ;
}

Jungle Roads---poj1251 hdu1301的更多相关文章

  1. 暑假集训(3)第二弹 -----Jungle Roads(Hdu1301)

    问题梗概:自从上次某个acmer来设计了拉格瑞圣岛的交通路线后,岛上的酋长就相当苦恼,他发现,虽然这些修好的公路便利了岛上的 交通,并且让拉格瑞圣岛的旅游业更加兴旺,甚至他们还收到了一笔不小的国际资金 ...

  2. 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 ...

  3. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

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

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

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

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

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

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

  7. HDU1301 Jungle Roads

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

  8. POJ1251 Jungle Roads Kruskal+scanf输入小技巧

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

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

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

  10. POJ 1251 Jungle Roads (prim)

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

随机推荐

  1. x264编码的图像出现乱码的问题

    将YUV进行x264编码的时候,建议将 i_threads 参数设置成 X264_SYNC_LOOKAHEAD_AUTO//* 取空缓冲区继续使用不死锁的保证. 否则有可能编码出来的数据会出现IDR_ ...

  2. 与MQ通讯的完整JAVA程序

    该程序实现了发送消息与读取消息的功能,见其中的 send***与get***方法.这只适合于测试,因为环境中的程序还需要对此有稍微的更改,在真实的环境中肯定是在while(true){...} 的无限 ...

  3. vc11(vs2012)下编译php源码

    需要原料: vs2012.php源码 1.本机的mingw没搞定,参考网上文章尝试vs2012编译,借助vs2012自带的命令行工具: 需要去bison官网下载bison.exe放在“c:/windo ...

  4. UITableView-FDTemplateLayoutCell 学习笔记

    本文转载至 http://www.tuicool.com/articles/I7ji2uM 原文  http://everettjf.github.io/2016/03/24/learn-uitabl ...

  5. Visual C++中去除警告

    在编程中,编译器警告的意思是提问程序员:如果这样做将会出现意外的错误,你确定要这样做吗? 在很多情况下,我们写程序的时候会出现一些警告,而这些警告我们都知道这样做的确是需要的并且程序中多处出现这种做法 ...

  6. Ubuntu VMware出现提示No 3D support is available的解决方法

    像我这样的Ubuntu脑残粉,电脑上只安装了Ubuntu,但是有时又必须得使用Windows,于是就装了一个Windows虚拟机,使用的是VMware,问题出在默认设置下启动虚拟机无法启动3D硬件加速 ...

  7. JS缺失错误- Uncaught SyntaxError: Unexpected token <

    这种情况,表明,缺少js文件 解决方式:在文件夹下将缺少js文件补足

  8. Mysql命令行导入sql数据

    mysqldump  是在  操作系统命令行下运行的,不是在 MySQL 命令行下运行的. 登陆数据库: 登陆本地mysql : mysql -h localhost -u root -p123456 ...

  9. 阿里云服务器如何设置IPV6通过appstore的审核

    苹果上架要求:要求支持IPV6only(因为阿里云主机没有IPV6only) 确认IPV6是否开启: 方式1:使用ifconfig查看自己的IP地址是否含有IPv6地址. 方式2.查看服务监听的IP中 ...

  10. python开发环境搭建(python2.7.5+pyCharm2.7.3)【原创】

    1.下载python 官网下载最新版python http://www.wingide.com/downloads 2.下载PyCharm 官网可下载最新版pyCharm-professional h ...