Truck History POJ - 1789 最小生成树之Kruskal算法

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on. 

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as 
1/Σ(to,td)d(to,td)

where the sum goes over all pairs of types in the derivation plan such that t  o is the original type and t  d the type derived from it and d(t  o,t d) is the distance of the types. 
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan. 

Input

The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.

Output

For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

题意:有大概2000辆卡车,卡车有一个长度为7的字符串标志,并且所有的卡车都是由第一辆卡车变换而来,变化开卡车需要distan,distan用卡车之间标识符中字符不相等的数量表示

输入由很多测试样例,以输入0结束,第一行一个数字代表卡车的数量,往下为n行,每行都有一个卡车的标识符,问变换出所有的卡车所需要的最小distan和为多少。
输出为:”The highest possible quality is 1/Q.“,Q为最小distan和。

思路:可以看出这是一个最小生成树的问题,因此我用的是Kruskal算法。本题首先要解决转换问题,首先将每个标识符都保存下来,然后遍历所有的标识符,将任意两个标识符之间的distan放入算法中的结构体中,剩下的就是按照
算法模板写就可以了。

代码:
  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;
 25     double cost;
 26 };
 27 //排序,从小到大
 28 bool cmp(node a,node b)
 29 {
 30     return a.cost<b.cost;
 31 }
 32 //存放边的信息
 33 vector<node> ve;
 34 
 35 //fa表示当前i的最远祖先
 36 int fa[2000];
 37 //初始化fa,开始时自己是自己的祖先
 38 void init(int qwq)
 39 {
 40     for(int i=0;i<=qwq;i++)
 41     {
 42         fa[i]=i;
 43     }
 44 }
 45 //查找最远祖先,同时进行路径压缩
 46 int find(int x)
 47 {
 48     if(fa[x]==x)
 49     {
 50         return x;
 51     }
 52     return fa[x]=find(fa[x]);
 53 }
 54 //判断最远祖先是否相同
 55 bool che(int x,int y)
 56 {
 57     return find(x)==find(y);
 58 }
 59 //合并x,y,把他们放到同一个家族中
 60 void mer(int x,int y)
 61 {
 62     if(!che(x,y)) 
 63     {
 64         fa[fa[x]]=fa[y];
 65     }
 66     return ;
 67 }
 68 int n,m;
 69 int main()
 70 {
 71     while(scanf("%d",&n)&&n!=0)
 72     {
 73         //初始化
 74         init(n);
 75         //char数组用于保存所有的标识符,还节省空间
 76         char ch[2005][8];
 77         for(int i=0;i<n;i++)
 78         {
 79             cin>>ch[i];
 80         }
 81         node no;
 82         //遍历所有标识符与其他标识符并将数据存入容器中
 83         for(int i=0;i<n-1;i++)
 84         {
 85             for(int j=i+1;j<n;j++)
 86             {
 87                 int s=0;
 88                 for(int k=0;k<7;k++)
 89                 {
 90                     if(ch[i][k]!=ch[j][k])
 91                     {
 92                         s++;
 93                     }
 94                 }
 95                 no.u=i;
 96                 no.v=j;
 97                 no.cost=s;
 98                 ve.push_back(no);
 99             }
100         }
101         //对容器进行排序
102         sort(ve.begin(),ve.end(),cmp);
103         int ans=0;
104 
105         for(int i=0;i<ve.size();i++)
106         {
107             if(!che(ve[i].u,ve[i].v))
108             {
109                 mer(ve[i].u,ve[i].v);
110                 ans+=ve[i].cost;
111             }
112         }
113         printf("The highest possible quality is 1/%d.\n",ans);
114         //清除容器,释放空间
115         ve.clear();
116     }
117 }
 

猜你喜欢

转载自www.cnblogs.com/mzchuan/p/11735057.html