题目描述:

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.

样例输入:
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
样例输出:
216
30
import java.io.IOException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.lang.Comparable;
import java.util.Arrays;class Edge implements Comparable<Edge>
{public int u, v, w;public Edge(int u, int v, int w) {this.u = u;this.v = v;this.w = w;}public int compareTo(Edge other) {return w - other.w;}
}class Main
{public static final boolean DEBUG = false;public static int[] p, rank;public static int find(int x){return (p[x] < 0) ? x : (p[x] = find(p[x]));}public static int Union(Edge e){int u = e.u, v = e.v;int pu = find(u), pv = find(v);if (pu != pv) {if (rank[pu] < rank[pv]) {p[pu] = pv;} else if (rank[pu] > rank[pv]) {p[pv] = pu;} else {p[pv] = pu;rank[pu]++;}return e.w;}return 0;}public static void main(String[] args) throws IOException{Scanner cin;int n;if (DEBUG) {cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));} else {cin = new Scanner(new InputStreamReader(System.in));}while (cin.hasNext()) {n = cin.nextInt();if (n == 0) break;TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();List<Edge> le = new ArrayList<Edge>();p = new int[n];rank = new int[n];Arrays.fill(p, -1);for (int i = 0; i < n - 1; i++) {String s = cin.next();char ch = s.charAt(0);int u, v, w;if (tm.containsKey(ch)) {u = tm.get(ch);} else {tm.put(ch, tm.size());u = tm.get(ch);}int cnt = cin.nextInt();for (int j = 0; j < cnt; j++) {s = cin.next();ch = s.charAt(0);w = cin.nextInt();if (tm.containsKey(ch)) {v = tm.get(ch);} else {tm.put(ch, tm.size());v = tm.get(ch);}le.add(new Edge(u, v, w));}}Collections.sort(le);int ans = 0;for (Edge e: le) {ans += Union(e);}System.out.println(ans);}}
} 

题目1154:Jungle Roads相关推荐

  1. Jungle Roads//最小生成树kruskal

    题目: Jungle Roads Time Limit: 2 Seconds      Memory Limit: 65536 KB The Head Elder of the tropical is ...

  2. HDU 1301 Jungle Roads(裸最小生成树)

    题目链接 今天做了好几个模版最小生成树...贴一个kurskral. 1 /* 2 HDU 1301 Jungle Roads 3 最小生成树Kurskal模版 4 */ 5 #include < ...

  3. 【HDU - 1301】Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法amp;amp;普里姆算法)

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

  4. Jungle Roads丛林道路(最小生成树PrimKruskal算法)

    Jungle Roads丛林道路 POJ - 1251 目录 Jungle Roads丛林道路 题意描述 Kruskal算法解题思路 Kruskal AC代码 Prim 解题思路 AC代码 The H ...

  5. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

  6. (kruskal)Jungle Roads

    题目 The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money wa ...

  7. [HDOJ1301]Jungle Roads

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 最小生成树 (Kruskal) 1 #pragma warning(disable:4996) ...

  8. POJ 1251 Jungle Roads

    题意:给你n个点  n-1行每行代表的是这个点到给定点的距离   求最短路 解题思路:开始是用getchar  发现runtime error   后来用了  字符串  才改进了   裸Kruskal ...

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

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

最新文章

  1. 在CentOS 7.5上升级SQLite3过程实录
  2. MCSE2003学习之六
  3. DataGridView删除、修改方法封装
  4. 系统安全运维 Server 2008 R2 事件查看器实现日志分析
  5. 室内主题元素分析图_2020届室内设计专业优秀毕业设计作品展(五)
  6. Elasticsearch 自定义分析器Analyzer
  7. 生成子集——二进制法
  8. linux ubuntu 获取ip,linux系统(ubuntu)怎么查看ip地址
  9. flexbox 弹性盒模型
  10. ajax 调用asp.net后台方法
  11. 【大数据部落】 17年房贷市场数据调研报告
  12. 全员系统的服务器地址,江西省全员人口信息系统登录(全员系统查询)
  13. java 课程设计题目_Java课程设计题目有哪些?Java课程设计题目汇总
  14. Android武林大会(转)
  15. 对于设计模式中七大原则的理解
  16. 【Unity3D实战】摇摆直升机开发实战(一)
  17. Spring Batch(三) 详细介绍Job Launcher、ItemReader、ItemProcessor、ItemWriter各个实现类和用途
  18. OpenShift 4 - 在单节点 OpenShift 上部署 ODF 存储软件
  19. 启用Direct3D功能
  20. 转换cdm为mysql_详解PowerDesigner之CDM、PDM、SQL之间转换

热门文章

  1. AutoCAD 命令参考手册
  2. 浅析C#中的套接字编程
  3. python下载安装教程2.7-Python2.7.6下载
  4. python好学嘛-爬虫Python入门好学吗?学什么?
  5. python做啥用-你都用 Python 来做什么?
  6. python开发工程师面试题-一名python web后端开发工程师的面试总结
  7. 自学python的书-推荐6本学习Python的免费电子书
  8. python怎么用excel-如何用python打开excel
  9. python爬虫代码1000行-最精简的爬虫 --仅需4行代码(python)
  10. python培训出来的有公司要吗-python培训机构出来好就业吗