Truck History
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 24724 |
|
Accepted: 9636 |
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 was 1/q.", where 1/q is the quality O f the best derivation plan.
Sample Input
4aaaaaaabaaaaaaabaaaaaaabaaaa0
Sample Output
The highest possible quality is 1/3.
Source
CTU Open 2003"Test Instructions" has n cars, each with a different string encoding, and all 7 bits. Defines the encoded distance as the number of positions of different characters in the encoded string (7 positions). In addition to the first truck, the other trucks are derived from another type of truck, and then define the pros and cons of the derivation scheme is the inverse of the total distance value, asking you how much the countdown is. "Analysis" is the most reciprocal, that is, the total distance value is the smallest, is obviously the smallest spanning tree, available prim.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<functional>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePi ACOs (-1.0)using namespaceStd;typedefLong Longll;Const intn=2005;Const intm=15005;intEdg[n][n];intLowcost[n];//record the minimum distance from the elements of I that are not joined to the tree collectionCharw[n][8];intn,m,t;intFunChar*a,Char*b) { intCnt=0; for(intk=0;k<7; k++){ if(A[k]!=b[k]) cnt++; }returnCNT;}voidBuild () { for(intI=0; i<n;i++) { for(intj=i+1; j<n;j++) {Edg[i][j]=edg[j][i]=Fun (W[i],w[j]); } }}voidPrim () {intsum=0; lowcost[0]=-1; for(intI=1; i<n;i++) {Lowcost[i]=edg[0][i]; } for(intI=1; i<n;i++){ intminn=inf,k; for(intj=0; j<n;j++){ if(lowcost[j]!=-1&&lowcost[j]<Minn) {k=j;minn=Lowcost[j]; }} sum+=Minn; LOWCOST[K]=-1; for(intj=0; j<n;j++) {Lowcost[j]=min (lowcost[j],edg[k][j]); }} printf ("The highest possible quality is 1/%d.\n", sum);}intMain () { while(~SCANF ("%d", &n) &&N) {memset (EDG,0,sizeof(EDG)); memset (Lowcost,0,sizeof(lowcost)); for(intI=0; i<n;i++) {scanf ("%s", W[i]); } Build (); Prim (); } return 0;}
View Code
POJ2032 Truck History (Prim)