首页 > 代码库 > poj 1789 Truck History

poj 1789 Truck History

Truck History

Time Limit: 2000MS  Memory Limit: 65536K

Description

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 to is the original type and td the type derived from it and d(to,td) 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.

题目大意:

  用一个7位的string代表一个编号,两个编号之间的距离代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的距离,现在要找出一个“衍生”方案,使得总代价最小。 N<=2000;

  这个“衍生”方案,实际上就是需要我们去构造一棵树。 原本的每一个编号就是图中的每一个点,点与点之间的连边,就是对应编号之间的距离。 

喜闻乐见的Code

 1 #include<algorithm> 2 #include<iostream> 3 using namespace std; 4 #define maxn 2005 5 #define maxm 4000000 6 struct Edge { 7     int u,v,w; 8     Edge(int u=0,int v=0,int w=0): 9         u(u),v(v),w(w) {}10     bool operator < (const Edge &a) const {11         return w<a.w;12     }13 }edge[maxm];14 int cnt,n,father[maxn];15 char node[maxn][10];16 int calc_w(int u,int v) {17     int w=0;18     for(int i=0;i<7;i++)19         if(node[u][i]!=node[v][i])20             w++;21     return w;22 }23 void Add_edge(int u,int v) {24     edge[++cnt]=Edge(u,v,calc_w(u,v));25     return;26 }27 int find(int x) {28     return father[x]==x?x:father[x]=find(father[x]);29 }30 bool Union(int x,int y) {31     int fx=find(x),fy=find(y);32     if(fx!=fy) {33         father[fx]=fy;34         return true;35     }36     return false;37 }38 int Kruskal() {39     int mst=0;40     sort(edge+1,edge+cnt+1);41     for(int  i=1;i<=cnt;i++)42         if(Union(edge[i].u,edge[i].v))43             mst+=edge[i].w;44     return mst;45 }46 int main() {47     ios::sync_with_stdio(false);//取消cin与stdio的同步;48     while(cin>>n&&n!=0) {49         for(int i=1;i<=n;i++) {50             cin>>node[i];51             father[i]=i;52         }53         for(int i=1;i<=n;i++)54             for(int j=1;j<=i;j++)55                 if(j!=i) Add_edge(i,j);56         cout<<"The highest possible quality is 1/"<<Kruskal()<<"."<<endl;57         cnt=0;58     }59     return 0;60 }

 

poj 1789 Truck History