Truck History Time limit:2000ms Memory limit:65536k Total submissions:24358 accepted:9476
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, the other for furniture, or for bricks. The company has it own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (all letter in each position has a very special meaning bu T is unimportant to this task). At the beginning'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.
Today, the ACM are rich enough to pay historians to study its history. One thing historians tried to find? called derivation plan-i.e. how the truck types the were. 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 H is not derived from any other type). The quality of a derivation plan is then defined as
1/σ (TO,TD) d (TO,TD)
Where the sum goes over all pairs of types in the derivation plan such, t o is the original type and T d the type Deri Ved from it and D (T o,t D) is the distance of the types.
Since historians failed, you are are to write a program to help them. Given The codes of truck types, your program should find the highest possible of a quality.
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). You may assume this 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.
The your program should output the text "the highest possible quality be 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
Click to open the topic link http://poj.org/problem?id=1789
As I understand it, the words say a lot, a bunch of boring people, to understand the history of trucks and so on. In fact, there is no egg to use. The point is to give you a n, the number of N Truck license plate, car brand only 7 characters, the number of each two of the corresponding digits in the code is called the length of the two codes. Because Quality=1/sum (T0,TD), you can see the minimum spanning tree by finding all the coded inheritance relationships that make the tree's weights (the number of different characters in the same bit) the smallest.
See Code BA:
#include <iostream> #include <stdio.h> #include <string.h> #include <queue> #include <
algorithm> #include <stdlib.h> #include <math.h> typedef long Long LL;
using namespace Std;
#define INF 0x3f3f3f3f #define N 2100 int n;
int maps[n][n],vis[n],dis[n];
void init () {int i,j;
for (i=0;i<n;i++) for (j=0;j<n;j++) maps[i][j]=maps[j][i]= (i==j)? 0:inf;
} void Prime () {int i,j,index,min,sum;
for (i=1;i<=n;i++) {dis[i]=maps[1][i];
vis[i]=0;
} vis[1]=1;
sum=0;
for (i=1;i<=n;i++) {min=inf;
Index=-1;
for (j=1;j<=n;j++) {if (!vis[j] && min>dis[j]) {min=dis[j];
Index=j;
} if (index==-1) break;
Vis[index]=-1;
Sum=sum+min; for (j=1;j<=n;j++) {if (!vis[j] && dis[j]>maps[index][j]) Dis[j]=maps[index][j];
} printf ("The highest possible quality is 1/%d.\n", sum);
int main () {int i,j,num,k;
Char str[n][10];
while (scanf ("%d", &n), N) {for (i=1;i<=n;i++) scanf ("%s", str[i));
Init ();
for (i=1;i<=n;i++) {for (j=i;j<=n;j++) {num=0;
for (k=0;k<7;k++) {if (Str[i][k]!= str[j][k]) num++;
} Maps[i][j]=maps[j][i]=num;
} Prime ();
return 0;
}