Jungle Roads POJ - 1251 简单最小生成数之Kruskal算法

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

翻译:有最多26个村庄,村庄之间用道路连接,道路是需要维修的,现在想问如果保持所有的村庄都能连接再一起即没有单独的村庄,要使道路的维护费用最少,则应该为多少,每个村庄最多与15个其他村庄直接连接,最多有75个道路
输入第有1到100个测试样例,以输入0结束,每个测试样例以n开头,表示有n个村庄村庄用字母表的前n个字母标记,大写。每个数据集都用n-1行完成,这些行以按字母顺序排列的村庄标签开始。最后一个村子没有线。一个村庄的每一
行以村庄标签开始,然后是从这个村庄到村庄的道路编号k,表示有几个村庄与之相连,然后是与之相连的村庄的编号和道路的维修费。

思路:求出在保持所有的的村庄相连的情况下,去掉一些花费比较大的道路,让剩下的n-1条道路的总花费最小。

代码:
  1 #include <cstdio>
  2 #include <fstream>
  3 #include <algorithm>
  4 #include <cmath>
  5 #include <deque>
  6 #include <vector>
  7 #include <queue>
  8 #include <string>
  9 #include <cstring>
 10 #include <map>
 11 #include <stack>
 12 #include <set>
 13 #include <sstream>
 14 #include <iostream>
 15 #define mod 998244353
 16 #define eps 1e-6
 17 #define ll long long
 18 #define INF 0x3f3f3f3f
 19 using namespace std;
 20 
 21 //u表示起点,v表示终点,cost表示花费
 22 struct node
 23 {
 24     int u,v,cost;
 25 };
 26 //排序,按花费小到大
 27 bool cmp(node a,node b)
 28 {
 29     return a.cost<b.cost;
 30 }
 31 //存放边的信息
 32 vector<node> ve;
 33 //n表示村庄的数量
 34 int m,n;
 35 //fa表示当前i的最远祖先
 36 //因为最多有26个村庄所以定义为30
 37 int fa[30];
 38 //初始化fa,开始时自己是自己的祖先
 39 void init(int qwq)
 40 {
 41     for(int i=0;i<=qwq;i++)
 42     {
 43         fa[i]=i;
 44     }
 45 }
 46 //查找最远祖先,同时进行路径压缩
 47 int find(int x)
 48 {
 49     if(fa[x]==x)
 50     {
 51         return x;
 52     }
 53     return fa[x]=find(fa[x]);
 54 }
 55 //判断最远祖先是否相同
 56 bool che(int x,int y)
 57 {
 58     return find(x)==find(y);
 59 }
 60 //合并x,y,把他们放到同一个家族中
 61 void mer(int x,int y)
 62 {
 63     if(!che(x,y)) 
 64     {
 65         fa[fa[x]]=fa[y];
 66     }
 67     return ;
 68 }
 69 
 70 int main()
 71 {
 72     while(scanf("%d",&n)&&n!=0)
 73     {
 74         node no;
 75         char a;
 76         int c,d;
 77         //初始化
 78         init(n);
 79         for(int i=1;i<n;i++)
 80         {
 81             cin>>a>>c;
 82             //由于输入是按顺序的
 83             //所以起点为i
 84             no.u=i;
 85             for(int j=0;j<c;j++)
 86             {
 87                 cin>>a>>d;
 88                 //终点转换成数字
 89                 no.v=a-'A'+1;
 90                 no.cost=d;
 91                 //放入
 92                 ve.push_back(no);
 93             }
 94         }
 95         //排序
 96         sort(ve.begin(),ve.end(),cmp);
 97         //结果
 98         int ans=0;
 99         for(int i=0;i<ve.size();i++)
100         {
101             //如果不在同一个集合里
102             if(!che(ve[i].u,ve[i].v))
103             {
104                 //合并
105                 mer(ve[i].u,ve[i].v);
106                 //加上这个道路的费用
107                 ans+=ve[i].cost;
108             }
109         }
110         printf("%d\n",ans);
111         //清除当前样例的数据
112         ve.clear();
113     }
114 }


猜你喜欢

转载自www.cnblogs.com/mzchuan/p/11720652.html
今日推荐