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

  1. 9
  2. A 2 B 12 I 25
  3. B 3 C 10 H 40 I 8
  4. C 2 D 18 G 55
  5. D 1 E 44
  6. E 2 F 60 G 38
  7. F 0
  8. G 1 H 35
  9. H 1 I 35
  10. 3
  11. A 2 B 10 C 40
  12. B 1 C 20
  13. 0

Sample Output

  1. 216
  2. 30
  3.  
  4. 最小生成树模板题
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<map>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<math.h>
  7. #define N 110
  8. #define INF 0xfffffff
  9.  
  10. using namespace std;
  11.  
  12. int dist[N], vis[N], n, maps[N][N];
  13.  
  14. void Init()
  15. {
  16. for(int i=; i<=n; i++)
  17. {
  18. vis[i] = ;
  19. dist[i] = INF;
  20. for(int j=; j<=n; j++)
  21. {
  22. maps[i][j] = INF;
  23. }
  24. }
  25. }
  26.  
  27. int Prim(int start)
  28. {
  29. int ans = ;
  30. for(int i=; i<=n; i++)
  31. dist[i] = maps[start][i];
  32. vis[start] = ;
  33. for(int i=; i<n; i++)
  34. {
  35. int Min = INF,index = -;
  36. for(int j=; j<=n; j++)
  37. {
  38. if(vis[j]== && Min > dist[j])
  39. Min = dist[j], index = j;
  40. }
  41. if(index == -) break;
  42. vis[index] = ;
  43. ans += Min;
  44. for(int j=; j<=n; j++)
  45. {
  46. if(vis[j]== && dist[j] > maps[index][j])
  47. {
  48. dist[j] = maps[index][j];
  49. }
  50. }
  51. }
  52. return ans;
  53. }
  54.  
  55. int main()
  56. {
  57. int i, j, x, a, b, ans, m;
  58. char s[];
  59. while(scanf("%d", &n), n)
  60. {
  61. Init();
  62. for(i=; i<n; i++)
  63. {
  64. scanf("%s%d", s, &m);
  65. a = s[] - 'A' + ;
  66. for(j=; j<m; j++)
  67. {
  68. scanf("%s%d", s, &x);
  69. b = s[] - 'A' + ;
  70. maps[a][b] = maps[b][a] = min(maps[a][b],x);
  71. }
  72. }
  73. ans = Prim();
  74. printf("%d\n", ans);
  75. }
  76. return ;
  77. }

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. MQTT-C-PUB

    /* ============================================================================ Name        : mqtest ...

  2. 《C++标准程序库》笔记之四

    本篇博客笔记顺序大体按照<C++标准程序库(第1版)>各章节顺序编排. ---------------------------------------------------------- ...

  3. Masonry — 使用纯代码进行iOS应用的autolayout自适应布局

    本文转载至   http://www.ios122.com/2015/09/masonry/ 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstrain ...

  4. var_dump出现省略号的问题

    xdebug.var_display_max_children=128xdebug.var_display_max_data=512xdebug.var_display_max_depth=5

  5. 获取访客IP、地区位置信息、浏览器、来源页面

    <?php //这个类似用来获取访客信息的 //方便统计 class visitorInfo { //获取访客ip public function getIp() { $ip=false; if ...

  6. 【技术分享会】 iOS开发简述

    前言 Objective-C(简称OC)也是面向对象的编程语言,运用的许多面向对象的编程思想和C# . Java .C++等变成语言都是相通的: 本次技术讲座主要讲一些设计模式.设计思想等计算机语言通 ...

  7. 原生js--cookie操作的封装

    封装cookie的操作:查询cookie个数.查询所有cookie的键.获取cookie.设置cookie.删除cookie.清除全部cookie /** * cookieStorage */func ...

  8. 怎么使用jstack精确找到异常代码

    1.代码demo //一个CPU密集型线程的demo: package chapter1; public class FindJavaThreadInTaskManager { public stat ...

  9. flex常用兼容写法

    一般放在common.css中: .flex{ display: -webkit-box; display: -webkit-flex; display: -moz-box; display: -ms ...

  10. How to Use Postman to Manage and Execute Your APIs

    How to Use Postman to Manage and Execute Your APIs Postman is convenient for executing APIs because ...