Truck history
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 19860 |
|
Accepted: 7673 |
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks is used for vegetable delivery, and for furniture, or for bricks. The company have its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (all letter in each position have a very special meaning bu T is the unimportant for this task). At the beginning of company's history, just a single truck type is used but later other types were derived from it, then From the new types another types were derived, and so on.
Today, ACM is a rich enough to pay historians to study it history. One thing historians tried to find out are 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 is derived from exactly one other truck type (except for the first truck type whic H is not derived from any other type). The quality of a derivation plan was and then defined as
1/σ (TO,TD) d (TO,TD)
Where the sum goes over all pairs of types in the derivation plan such, that's the original type and TD the type derive D from it and D (TO,TD) is the distance of the types.
Since historians failed, you is to the write a program to the 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). Assume that the codes uniquely describe the trucks, i.e., No. Of these N lines is 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 are 1/q", where 1/q is the Qu Ality of the best derivation plan.
Sample Input
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output
The highest possible quality is 1/3.
Source CTU Open 2003
Topic Link: http://poj.org/problem?id=1789
Title: Give n a string of the same length, a string representing a point, Each of the two strings has a different number of characters, then the difference is the distance between two points, requiring each point is connected to the maximum value of quality
Topic Analysis: Quality formula has been given, we can find to let quality, the more the denominator is smaller, And the problem is transformed into the minimum spanning tree problem, the Kruskal algorithm runs the answer comes out.
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
int const MAX = 2005;
int Fa[max];
Char s[max][10];
int n, m; struct Edge {int u, V, W;}
E[max * MAX/2];
int CMP (Edge A, Edge b) {return A.W < B.W;}
void Uf_set () {for (int i = 0; i < MAX; i++) fa[i] = i;}
int Find (int x) {return x = = Fa[x]? x:fa[x] = Find (Fa[x]);}
void Union (int a, int b) {int r1 = Find (a);
int r2 = Find (b);
if (r1! = r2) FA[R2] = R1;
} void Get_map () {for (int i = 0, i < n; i++) {for (int j = i + 1; j < N; j + +) {
int cnt = 0;
for (int k = 0; k < 7; k++) cnt + = (s[i][k]! = S[j][k]);
E[M].U = i;
E[M].V = j;
E[M++].W = CNT;
}}} int Kruskal () {int num = 0, sum = 0;
Uf_set ();
for (int i = 0; i < m; i++) {int u = e[i].u;
int v = E[I].V; if (Find (U) ! = Find (v)) {Union (U, v);
sum + = E[I].W;
num++;
} if (num >= n-1) break;
} return sum;
} int main () {while (scanf ("%d", &n)! = EOF && N) {for (int i = 0; i < n; i++)
scanf ("%s", S[i]);
m = 0;
Get_map ();
Sort (E, E + M, CMP);
printf ("The highest possible quality is 1/%d.\n", Kruskal ()); }
}