最小生成树

逃离我推掉我的手 2021-11-29 22:28 560阅读 0赞

Jungle Roads

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

  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

标准最小生成树,用了简化版的Prim算法,也可以用Kruskal算法

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3 #include <algorithm>
  4. 4 #define maxn 65535
  5. 5 using namespace std;
  6. 6 int MG[1005][1005];
  7. 7 int F[1005];
  8. 8
  9. 9 int main()
  10. 10 {
  11. 11 //freopen("sample.txt","r",stdin);
  12. 12 int n;
  13. 13 while(~scanf("%d",&n)&&n)
  14. 14 {
  15. 15 int sum=0,i;
  16. 16 memset(MG,0,sizeof(MG));
  17. 17 for(i=0;i<n-1;i++)
  18. 18 {
  19. 19 char c;
  20. 20 scanf(" %c",&c);
  21. 21 int a;
  22. 22 scanf("%d",&a);
  23. 23 for(int j=0;j<a;j++)
  24. 24 {
  25. 25 char k;
  26. 26 int b;
  27. 27 scanf(" %c",&k);
  28. 28 scanf("%d",&b);
  29. 29 MG[c-'A'][k-'A']=b;
  30. 30 MG[k-'A'][c-'A']=b;
  31. 31 }
  32. 32 F[i]=maxn;
  33. 33 }
  34. 34 F[i]=maxn;
  35. 35 F[0]=0;
  36. 36 for(i=1;i<n;i++)
  37. 37 {
  38. 38 if(MG[0][i])
  39. 39 {
  40. 40 F[i]=MG[0][i];
  41. 41 }
  42. 42 }
  43. 43 for(i=1;i<n;i++)
  44. 44 {
  45. 45 int m=maxn,t;
  46. 46 for(int g=0;g<n;g++)
  47. 47 {
  48. 48 if(F[g]&&F[g]<m)
  49. 49 {
  50. 50 t=g;
  51. 51 m=F[g];
  52. 52 }
  53. 53 }
  54. 54 sum+=F[t];
  55. 55 F[t]=0;
  56. 56 for(int j=1;j<n;j++)
  57. 57 {
  58. 58 if(F[j]&&MG[t][j])
  59. 59 {
  60. 60 F[j]=min(MG[t][j],F[j]);
  61. 61 }
  62. 62 }
  63. 63 }
  64. 64 printf("%d\n",sum);
  65. 65 }
  66. 66 return 0;
  67. 67 }

转载于:https://www.cnblogs.com/jiamian/p/11186654.html

发表评论

表情:
评论列表 (有 0 条评论,560人围观)

还没有评论,来说两句吧...

相关阅读

    相关 生成

    最小生成树定义: 在一给定的无向图 G = (V, E) 中,(u, v) 代表连接顶点 u 与顶点 v 的边(即![(u, v)\\in E][u_ v_in E]),而

    相关 生成

    一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边。 最小生成树在n个顶点的情形下,有n-1条边。生成树是对连

    相关 生成

    有n个城市,m条道路,现在输入n到m的道路的长度,用最少的边将图连接,输出让图连接的最小值 这道题我研究了好长时间才把答案看明白,现在给大家分享一下 具体代码如下

    相关 生成

    问题提出: 要在n个城市间建立通信联络网。顶点:表示城市,权:城市间通信线路的花费代价。希望此通信网花费代价最小。 问题分析: 答案只能从生成树中找,因为要做到任何

    相关 生成

    kruskal算法基本思路:先对边按权重从小到大排序,先选取权重最小的一条边,如果该边的两个节点均为不同的分量,则加入到最小生成树,否则计算下一条边,直到遍历完所有的边。 p

    相关 生成

    最小生成树 什么是最小生成树 给定一张无向带权图G=(V,E,W),从中挑选出|V|-1条边,使得V中任意两点都有直接或者间接的路径 显然,最后得到的子