Truck History Poj 1789 (Prim)

Source: Internet
Author: User
Tags lowercase
Truck history
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 21311 Accepted: 8277

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 T o are the original type and T d the Type de Rived from it and D (T o,t D) are 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.



It is difficult to feel this problem on the map. I actually put map[i][j]=map[j][i]=* into the map[i][j]=*, in the brain after 0.1 seconds of thinking is not necessarily the first input is derived root q Q.


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define INF

0x3f3f3f3f using namespace std;
Char S[2001][8];
int map[2001][2001],ans,vis[2001],dis[2001],n,z; void Prim () {    int i,j,k,pos,ma;     memset (vis,0,sizeof (Vis));     for
(I=1; i<n; i++)         dis[i]=map[0][i];
    vis[0]=1;
    for (i=0; i<n-1; i++)     {        ma=inf;         for (j=1; j<n; j + +)         {             if (Dis[j]<ma&&!vis[j])              {         
       Ma=dis[j];                 pos=j;
           }        }
        Z+=ma;
        vis[pos]=1;         for (j=1; j<n; j + +)         {             if (dis[J]>map[pos][j] &&!vis[j])              {        
        Dis[j]=map[pos][j];
           }        }
   }     printf ("The highest possible quality is 1/%d.\n", z); } int main () {    int i,j,k,m,mi;     Ios::sync_with_stdio (false);     while (cin>>n&&n)     {        z=0;          for (i=0; i<n; i++)             cin>>s[
I];
        memset (map,0,sizeof (MAP));         for (i=0; i<n; i++)         {             for (j=0; j<n; j + +)              if (i!=j)            
    Map[j][i]=map[i][j]=inf;             Else       
         map[i][j]=0;        }         for (i=0; i<n; i++)          {            ans=0;              for (j=0; j<n; j + +)              {               
ans=0;
                for (k=0; k<7; k++)                 {                     if (s[i][k]!=s[ J][K])                      {                
        ans++;                    }                 }                  if (Map[i][j]>ans)                  {              
      Map[i][j]=ans;                   
  Map[j][i]=ans;                }                 //    Cout<<map[i]
[j]<<endl;
           }        }         Prim ();
   }     return 0;


 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.