最小生成树 逃离我推掉我的手 2021-11-29 22:28 420阅读 0赞 # Jungle Roads # [http://poj.org/problem?id=1251][http_poj.org_problem_id_1251] ### Description ### ![1251_1.jpg][] 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 标准最小生成树,用了简化版的Prim算法,也可以用Kruskal算法 ![ContractedBlock.gif][] ![ExpandedBlockStart.gif][] 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #define maxn 65535 5 using namespace std; 6 int MG[1005][1005]; 7 int F[1005]; 8 9 int main() 10 { 11 //freopen("sample.txt","r",stdin); 12 int n; 13 while(~scanf("%d",&n)&&n) 14 { 15 int sum=0,i; 16 memset(MG,0,sizeof(MG)); 17 for(i=0;i<n-1;i++) 18 { 19 char c; 20 scanf(" %c",&c); 21 int a; 22 scanf("%d",&a); 23 for(int j=0;j<a;j++) 24 { 25 char k; 26 int b; 27 scanf(" %c",&k); 28 scanf("%d",&b); 29 MG[c-'A'][k-'A']=b; 30 MG[k-'A'][c-'A']=b; 31 } 32 F[i]=maxn; 33 } 34 F[i]=maxn; 35 F[0]=0; 36 for(i=1;i<n;i++) 37 { 38 if(MG[0][i]) 39 { 40 F[i]=MG[0][i]; 41 } 42 } 43 for(i=1;i<n;i++) 44 { 45 int m=maxn,t; 46 for(int g=0;g<n;g++) 47 { 48 if(F[g]&&F[g]<m) 49 { 50 t=g; 51 m=F[g]; 52 } 53 } 54 sum+=F[t]; 55 F[t]=0; 56 for(int j=1;j<n;j++) 57 { 58 if(F[j]&&MG[t][j]) 59 { 60 F[j]=min(MG[t][j],F[j]); 61 } 62 } 63 } 64 printf("%d\n",sum); 65 } 66 return 0; 67 } 转载于:https://www.cnblogs.com/jiamian/p/11186654.html [http_poj.org_problem_id_1251]: http://poj.org/problem?id=1251 [1251_1.jpg]: /images/20211129/3571b2be848e4e97b2860183bae0ce52.png [ContractedBlock.gif]: https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif [ExpandedBlockStart.gif]: /images/20211129/4d389c4c759b47fdbef8ad5c4d690e2c.png
相关 最小生成树 最小生成树定义: 在一给定的无向图 G = (V, E) 中,(u, v) 代表连接顶点 u 与顶点 v 的边(即![(u, v)\\in E][u_ v_in E]),而 末蓝、/ 2022年09月20日 15:18/ 0 赞/ 200 阅读
相关 生成树相关问题(最小生成树变形,次小生成树,最小度限度生成树,极差最小生成树) 生成树相关问题(最小生成树变形,次小生成树,最小度限度生成树,极差最小生成树) 视频:[https://www.bilibili.com/video/BV1G64y187ke Bertha 。/ 2022年09月12日 05:52/ 0 赞/ 201 阅读
相关 最小生成树 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边。 最小生成树在n个顶点的情形下,有n-1条边。生成树是对连 忘是亡心i/ 2022年08月03日 05:28/ 0 赞/ 218 阅读
相关 最小生成树 邻接矩阵建图+prim朴素[算法][Link 1] 代码通过HDU1102 \[cpp\] [view plain][] [copy][view plain] 1. 超、凢脫俗/ 2022年06月09日 04:27/ 0 赞/ 69 阅读
相关 最小生成树 有n个城市,m条道路,现在输入n到m的道路的长度,用最少的边将图连接,输出让图连接的最小值 这道题我研究了好长时间才把答案看明白,现在给大家分享一下 具体代码如下 爱被打了一巴掌/ 2022年05月30日 08:56/ 0 赞/ 178 阅读
相关 最小生成树 设G = (V,E)是无向连通带权图,即一个网络。E中的每一条边(v,w)的权为c\[v\]\[w\]。如果G的子图G’是一棵包含G的所有顶点的树,则称G’为G的生成树。生成 红太狼/ 2022年05月24日 11:41/ 0 赞/ 264 阅读
相关 最小生成树 问题提出: 要在n个城市间建立通信联络网。顶点:表示城市,权:城市间通信线路的花费代价。希望此通信网花费代价最小。 问题分析: 答案只能从生成树中找,因为要做到任何 雨点打透心脏的1/2处/ 2022年03月18日 12:28/ 0 赞/ 288 阅读
相关 最小生成树 kruskal算法基本思路:先对边按权重从小到大排序,先选取权重最小的一条边,如果该边的两个节点均为不同的分量,则加入到最小生成树,否则计算下一条边,直到遍历完所有的边。 p ゝ一纸荒年。/ 2022年02月25日 14:20/ 0 赞/ 267 阅读
相关 最小生成树 最小生成树 什么是最小生成树 给定一张无向带权图G=(V,E,W),从中挑选出|V|-1条边,使得V中任意两点都有直接或者间接的路径 显然,最后得到的子 浅浅的花香味﹌/ 2021年12月22日 01:57/ 0 赞/ 321 阅读
相关 最小生成树 Jungle Roads [http://poj.org/problem?id=1251][http_poj.org_problem_id_1251] Descrip 逃离我推掉我的手/ 2021年11月29日 22:28/ 0 赞/ 421 阅读
还没有评论,来说两句吧...