POJ 1789 Truck History (minimum spanning tree) __ graph theory

Source: Internet
Author: User
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 out are so called derivation plan–i.e. How the truck types. 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) 1/σ (TO,TD) d (TO,TD)

Where the sum goes over all pairs of types in the derivation plan such the the 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 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.


Output

For each test case, your program should output the text "the highest possible quality be 1/q.", where 1/q is the quality O f the best derivation plan.


Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0


Sample Output

The highest possible quality is 1/3.


the

A 7-bit string represents a number, and the distance between the two numbers represents the number of different letters between the two numbers. A number can only be derived from another number, the price is the corresponding distance between the two numbers, now to find a derivative scheme, so that all the numbers can be directly or indirectly form the conversion, and the total cost of the smallest, that is, distance and the minimum.


train of Thought

Since all numbers have a conversion weight, it can be represented as a complete picture.

Requirements can be converted directly or indirectly, that is, the need to find a subgraph must be connected.

The minimum weight, that is, the minimum spanning tree.


AC Code

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <map

> #include <algorithm> using namespace std;
Char str[2005][10];
int parent[2005],n,etop;
    struct Eage//Edge structure, U, V for both ends, W for edge weights {int U, V, W;
    Eage () {};
        eage (int u,int v,int W) {this->u=u;
        this->v=v;
    this->w=w;
}} eg[2005*2005];
    int jud (int a,int b) {int ans=0;
    for (int i=0; i<7; i++) if (str[a][i]!=str[b][i)) ans++;
return ans;

BOOL CMP (Eage A, eage b)//Sort calls {return A.W < B.W;}
    int find (int x)//Look for root node to determine whether the basis of the same tree {if (parent[x] = = 1) return x;
Return find (Parent[x]);
    } void Kruskal ()//kruskal algorithm, parent can restore a spanning tree, or forest {memset (parent,-1, sizeof (parent));        int cnt = n,ans=0;    The initial number of root nodes is n sort (EG, eg+etop, CMP);     By weight the edge is sorted from small to large for (int i = 0; i < etop i++)///by weight from small to large selection edge {if (cnt = 1) break; WhenOnly 1 root nodes, jump out of the loop int t1 = find (eg[i].u), t2 = find (EG[I].V);
            if (T1!= T2)//If not the same tree, select the Edge, {ans = EG[I].W;
            PARENT[T1] = T2;      cnt--;
Each merge reduces one root node} printf ("The highest possible quality is 1/%d.\n", ans);
        int main () {while (~scanf ("%d%*c", &n) &&n) {etop=0;
        for (int i=0; i<n; i++) gets (str[i));
                for (int i=0; i<n; i++) for (int j=i+1; j<n; J + +) {int Cnt=jud (I,J);
                Eg[etop++]=eage (I,J,CNT);
            Eg[etop++]=eage (J,I,CNT);
    } Kruskal ();
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.