Truck history
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:17610 |
|
Accepted:6786 |
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 (role t for the first truck type which was not derived from any other type ). the quality of a derivation plan was then defined
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 shocould 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 shocould output the text "the highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.
Sample Input
4aaaaaaabaaaaaaabaaaaaaabaaaa0
Sample output
The highest possible quality is 1/3.
A 7-digit string represents a number, and the distance between two numbers represents the number of different letters between the two numbers.
One serial number can only be derived from another serial number. The price is the corresponding distance between these serial numbers,
Now we need to find a "derivative" scheme to minimize the total cost, that is, the sum of distance.
The key to this question is to convert the problem into a minimal spanning tree.
Each vertex is a vertex of the graph. The difference between the vertex and the vertex is the weight of this edge. What we need in the question is to find the Minimum Spanning Tree.
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<vector>#include<queue>#define INF 0x3f3f3fusing namespace std;const int maxn = 2000 + 50;int f[maxn];int map[maxn][maxn];bool vist[maxn];char str[maxn][10];int ans[maxn];int n;int find(int x, int y){ int cnt = 0; for(int i=0; i<7; i++) if( str[x][i]!=str[y][i] ) cnt++; return cnt;}void init(){ memset( map, 0, sizeof(map) ); memset( vist, false, sizeof(vist) ); memset( ans, 0, sizeof(ans) ); for(int i=0; i<n; i++) scanf( "%s", str[i] ); //for(int i=0; i<n; i++) // printf("%s\n", str[i]); for(int i=0; i<n; i++) for(int j=0; j<=i; j++) map[i][j] = map[j][i] = find(i, j);}void prim(){ int minc, mind; vist[0] = true; ans[0] = 0; for(int i=1; i<n; i++) ans[i] = map[0][i]; for(int j=0; j<n-1; j++) { minc = INF; for(int i=0; i<n; i++) { if( !vist[i] && minc>ans[i] ) { minc = ans[i]; mind = i; } } if(minc != INF) { vist[mind] = true; for(int i=0; i<n; i++) if( !vist[i] && ans[i]>map[mind][i] ) ans[i] = map[mind][i]; } }}void output(){ int sum = 0; for(int i=1; i<n; i++) sum += ans[i]; printf("The highest possible quality is 1/%d.\n", sum);}int main(){ while( scanf( "%d", &n )==1 &&n ) { init(); prim(); output(); } return 0;}