Jungle Roads
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 19454 | Accepted: 8919 |
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
题意:
给出 n 个地方,后给出每个地方连接情况,求最小生成树。
思路:
最小生成树 Prim 算法 + 并查集。
AC:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef struct { int a, b, w; } node; node no[30 * 30]; int num[30], root[30]; bool cmp (node a, node b) { return a.w < b.w; } int Find (int x) { return x == root[x] ? x : root[x] = Find(root[x]); } int main() { int n; while (~scanf("%d", &n) && n) { for (int i = 0; i < n; ++i) { num[i] = 0; root[i] = i; } int e = 0; for (int i = 0; i < n - 1; ++i) { char f; int ans; scanf(" %c %d", &f, &ans); while (ans--) { char t; scanf(" %c %d", &t, &no[e].w); no[e].a = f - 'A'; no[e++].b = t - 'A'; } } sort(no, no + e, cmp); int sum = 0; for (int i = 0; i < e; ++i) { int A = Find(no[i].a); int B = Find(no[i].b); if (A != B) { sum += no[i].w; root[A] = B; } } printf("%d\n", sum); } return 0; }
相关推荐
本资源提供了关于最小生成树和并查集的题目列表,共40道题目,涵盖了基础最小生成树、基础并查集、枚举最小生成树、同构图、离线并查集、dfs 枚举组合情况、最大生成树、带权并查集、异或并查集、斯坦纳树、LCA等...
【Audio Jungle.mp3(纯净人声) 去Audio Jungle水印】 Audio Jungle是一个知名的在线音乐市场,专门提供高质量的原创音乐和音效素材,供创作者、制作人以及媒体专业人士使用。这个标题提及的"Audio Jungle.mp3"很...
Jungle Roads问题与最小生成树密切相关。在图论中,最小生成树是连接图中所有顶点的一组边,其总权重最小。Prim算法和Kruskal算法是两种常用的求解方法,它们都确保了生成树的最小权重,但实现策略不同,Prim从一个...
AudioJungle是一个知名的在线市场,专门提供高质量的音乐、音效和配乐,供创作者们用于他们的项目。然而,AudioJungle提供的音频文件通常带有水印,这是为了保护创作者的版权,防止未经授权的使用。在某些情况下,如...
懂的都懂,可以通过adobe 的audition 去掉 audio jungle 的水印 选择一段来自audiojungle.net的音频塑材,打开au,效果,降噪/恢复 声音移除,然后再也听不到那个女人的声音了
#### 2421 *Constructing Roads - 最小生成树 - **知识点**:最小生成树算法。 #### 2446 Chessboard - 二分匹配 - **知识点**:二分匹配算法。 #### 2455 Secret Milking Machine - 网络流 - **知识点**:网络...
本文将围绕如何处理AudioJungle音乐文件中的水印进行探讨,并介绍相关的音频编辑工具和技术。 首先,我们需要一个强大的音频编辑软件,如Audacity。Audacity是一款免费、开源的跨平台音频编辑和录音软件,支持多种...
本srm模型为高解析版的Audiojungle声音水印特别处理过的声音模型,理论上支持各种版本的AU去消除Audiojungle的声音水印,操作方式自行查找
《Jungle卡通》是一款以森林动物为主题的字体设计,它将卡通元素与文字艺术巧妙结合,为设计师们提供了独特的视觉表达工具。在IT行业中,字体设计是界面设计、广告设计、网页设计等多个领域不可或缺的重要组成部分,...
AudioJungle水印去除文件,用于识别人声,后期音频水印处理。详情可见:https://dengxj.blog.csdn.net/article/details/103193336
安装后,就可以在自己的Python项目中导入并使用"jungle"库提供的功能。 总的来说,“jungle”是一个Python库,通过PyPI分发,用户可以通过下载源代码压缩包,然后解压和安装来使用。这个库的具体功能和用法,需要...
**Jungle Scout 亚马逊插件**是一款专门为亚马逊平台卖家设计的强大工具,旨在通过数据驱动的方式帮助卖家进行高效、精准的选品策略。这款插件涵盖了选品、竞品跟踪、市场趋势分析等多个关键环节,是亚马逊卖家提升...
在音频处理领域,Audio Jungle是一个知名的在线市场,提供各种音乐、音效和声音设计素材。然而,这些素材往往带有特定的水印,主要是为了保护版权,防止未经授权的使用。水印通常是在音频的背景中添加一种低音量的...
去除Audio Jungle水印,这个文件是Audio Jungle音频素材中的纯净人声,自行百度去水印方法,去除水印必须有纯净的人声,这是去水印必备的
Jungle Defence is a game where you have to withstand waves of enemy attack to protect you base in the jungle.
jungle scout pro 跨境电商亚马逊必备工具 抓取产品大数据
"Jungle"这个标题可能暗示了一款与自然、野生动物或热带森林主题相关的字体集合。这类字体通常具有独特的形状,可能包含手绘元素,以创造一种原始、野性或生机勃勃的氛围。 描述中同样提到了"Jungle",这进一步强化...
Unity Asset - 卡通原始森林(Stylized Jungle Pack.unitypackage)
Unity Jungle War:直接使用Socket_TCP开发在线游戏_ Jungle War